2

I'm using the minted package to generate syntax-highlighted code snippets. The listing I want to show has some lines that overflow, though. Minted can automatically handle line breaks with the breakline option. But I want the line breaks to appear on the same level of indentation as the actual line.

I thought the breakautoindent option would do exactly what I wanted, but it doesn't indent the line break, no matter what the indent of the line is. The following code

\begin{minted}[linenos, breaklines, breakautoindent=true]{AMPL}
param n;  # Number of nodes in network

set NODES := 1..n;
set LINKS within (NODES cross NODES);

param Capacities {LINKS} >= 0;  # Maximum capacity for each edge


var Flow {(i, j) in LINKS} >= 0, <= Capacities[i,j];  
                            # Restrain the domain of the variables to [0, capacity]

maximize MaxFlow:  # Objective function (flow recieved by end node)
    sum {(i, n) in LINKS} Flow[i, n];



subject to Conservation:  # Ensure no flow is lost from start to end
    sum {(1, j) in LINKS} Flow[1, j] = sum {(i, n) in LINKS} Flow[i, n];

subject to Balance {k in (NODES diff {1, n})}:  # Transit nodes
    sum {(k, j) in LINKS} Flow[k,j]  == sum {(i, k) in LINKS} Flow[i, k];
\end{minted}

produces

Output

Notice that the line breaks have all the same level of indentation (i.e. none), but there are some lines that have a much higher indentation (e.g. the # Restrain [...] comment).

What is the source of the problem?

Edit: I've also tried obeytabs.

1

The Pygments AMPL lexer treats whitespace as Pygments Text.Whitespace tokens, which are "for specially highlighted whitespace". As a result, indentation appears not as plain indentation characters (which is what minted's breaklines works with), but rather as those characters wrapped in a Pygments token macro (in this case, \PYG{+w}{<whitespace_chars>}).

The easiest solution is to edit ampl.py to replace (r'\s+', Text.Whitespace) with (r'\s+', Text). It would technically be possible for minted to look for whitespace at the start of the first token in each line, but implementing that correctly (particularly if the token type needs to be taken into account) might be complicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.