minted icon indicating copy to clipboard operation
minted copied to clipboard

\inputminted with relevant snips?

Open NAThompson opened this issue 5 years ago • 3 comments

Suppose I have a code example foo.cpp

int main() {
      distracting_preliminaries();
      relevant_code();
      irrelevant_teardown();
}

When I use \inputminted{cpp}{foo.cpp}, I get all the code included, not just the relevant_code(). Of course, I could just do a \begin{minted}{cpp} relevant_code()\end{minted}, but then I'd lose the benefits of running CI on the snip.

Is there a syntax for \inputminted to snip out relevant code? For example

int main() {
      distracting_preliminaries();
      //minted_relevant
      relevant_code();
       //end_minted_relevant
      irrelevant_teardown();
}

which would extract relevant_code() into my document?

NAThompson avatar May 30 '19 18:05 NAThompson

I don't think minted supports this (I have v2.5), but it would be a nice feature to have.

As a possible workaround (I have never tried it), the FAQ mentions the ability to run an arbitrary script instead of pygmentize by setting

\renewcommand{\MintedPygmentize}{...}

You could write a script that strips everything outside of the "relevant" flags, and runs pygmentize on the code that remains.

mbaz avatar May 30 '19 18:05 mbaz

You can use firstline and lastline to display only a range of lines. There is no support for including part of a file based on starting/ending strings or regex. This has been requested in the past, and is on the list of features to add someday. Unfortunately, getting this (and several other more advanced features) to work will probably have to wait on some relatively major refactoring and reimplementations.

gpoore avatar May 30 '19 20:05 gpoore

@gpoore : Well, in any case, your efforts on this project are highly appreciated. This is a really useful project.

NAThompson avatar May 30 '19 20:05 NAThompson

minted version 3.0 is now under development, thanks to a grant from the TeX Users Group. It will include support for including snippets of external files based on regular expressions/literal delimiters. Progress on including snippets will be tracked in #373 . Initial beta releases of minted version 3.0 are expected by early 2024.

gpoore avatar Sep 12 '23 17:09 gpoore