minted
minted copied to clipboard
\inputminted with relevant snips?
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?
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.
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 : Well, in any case, your efforts on this project are highly appreciated. This is a really useful project.
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.