minted icon indicating copy to clipboard operation
minted copied to clipboard

Include code snippet based on comment marks

Open lahwaacz opened this issue 9 years ago • 5 comments

I know it is possible to include only a part of a file based on the firstline/lastline options, but this is very inflexible. The more flexible alternative would be to specify a syntax to identify the snippets dynamically based on named marks given in the source code comments, e.g.

//@@snippet_begin(helloworld)
using namespace std; 
cout << "Hello World!" << endl;
//@@snippet_end

(example borrowed from [1])

Would it be possible to implement this functionality? Or are there already some alternatives that provide this functionality, perhaps in combination with minted?

lahwaacz avatar May 04 '15 17:05 lahwaacz

Internally, minted uses fancyvrb to typeset code. fancyvrb can typeset only between a specified start and end string (literal string, first occurrence). From the fancyvrb docs:

\newcommand*\FancyVerbStartString{FROM}
\newcommand*\FancyVerbStopString{TO}
\begin{Verbatim}
First verbatim line.
FROM
Second verbatim line.
TO
Third verbatim line.
\end{Verbatim}

Using \FancyVerbStartString etc. probably works already, but it's a clunky interface, and the macros have to be \let to \relax manually when they are no longer desired. However, it shouldn't be too hard to write a nice wrapper for this functionality. I'm hoping to release minted 2.1 within the next fews weeks, and can probably include an interface for literal start/end strings in that.

The proper solution would be to use a regex to extract the relevant code. The next major version of PythonTeX will have this functionality, and once I get it working there, it shouldn't be hard to port over to minted. I don't have a good estimate on when that might arrive, except for before the end of the summer and ideally by the end of June.

gpoore avatar May 04 '15 17:05 gpoore

Thanks for considering this, looking forward to the next release!

lahwaacz avatar May 04 '15 20:05 lahwaacz

I also like that feature. I first saw something like that implemented in the Boost documentation, e.g. http://www.boost.org/doc/libs/1_58_0/libs/spirit/example/qi/num_list1.cpp with //[tutorial_numlist1.

Oberon00 avatar May 04 '15 21:05 Oberon00

I developed a rough and limited method to do so. Basically I defined a new command which takes the FROM and TO labels (#1 and #2) as input. Then I execute a sed command on the file I want to include (PATHTOFILE) in order to extract the selected lines and put them in a temporary file (PATHTOTEMPFILE). I can then call \inputminted on PATHTOTEMPFILE and finally I can delete PATHTOTEMPFILE.

\newcommand{\inputpartmint}[2]{%
\immediate\write18{sed -n '/#1/,/#2/p' "PATHTOFILE" > PATHTOTEMPFILE}%
\inputminted{LANGUAGE}{PATHTOTEMPFILE}%
\immediate\write18{rm PATHTOTEMPFILE}%  
}

I know this is a limited solution, but I hope it may help...

AntoniniP avatar Dec 20 '15 11:12 AntoniniP

Another workaround is described here: http://tex.stackexchange.com/questions/130738/formatting-code-fragments-extracted-from-file-with-minted. hth.

nicolaspayette avatar May 31 '16 21:05 nicolaspayette

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

Inspired by the snippet posted by @nicolaspayette, I wrote a script that takes care of this, and also sets highlightlines based on comments as well: https://github.com/The-Compiler/latex-minted-extract

It currently only works for # ... style comments since that's what I needed it for (had to do a couple of big changes to my example code before minted 3 was out, and this made things a lot easier). However, adjusting it to work with // as well should be a trivial change if someone is interested in giving it a try.

The-Compiler avatar Mar 09 '24 11:03 The-Compiler