TransWikia.com

lstinline with special characters breaks some environments (runaway argument)

TeX - LaTeX Asked on September 3, 2021

I am trying to enclose an inline listing with some environment like fbox or centerline. My MWE below illustrates how lstinline normally escapes the % and { characters but inside the above-mentioned environments I get errors. How can I solve that?

Interestingly, other environments like tcolorbox this works correctly. What’s the difference here?

documentclass{article}
usepackage[utf8]{inputenc}
usepackage{listings}

begin{document}

lstinline!This is ok!

fbox{% No problem here
lstinline!This is ok!
}

lstinline!This % does not cause problems normally!

lstinline!Or { this}!

fbox{% Error here
lstinline!But % this does!
}

centerline{lstinline!This is also ok!}

centerline{lstinline!But not { this!}

end{document}

Log (slightly trimmed):

This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex 2019.12.13)  25 JUL 2020 18:44
entering extended mode
 write18 enabled.
 %&-line parsing enabled.
**main.tex
(/compile/main.tex
LaTeX2e <2019-10-01> patch level 3
(/usr/local/texlive/2019/texmf-dist/tex/latex/base/article.cls
Document Class: article 2019/10/25 v1.4k Standard LaTeX document class
(/usr/local/texlive/2019/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2019/10/25 v1.4k Standard LaTeX file (size option)
)
c@part=count80
c@section=count81
c@subsection=count82
c@subsubsection=count83
c@paragraph=count84
c@subparagraph=count85
c@figure=count86
c@table=count87
abovecaptionskip=skip41
belowcaptionskip=skip42
bibindent=dimen102
) (/usr/local/texlive/2019/texmf-dist/tex/latex/base/inputenc.sty
Package: inputenc 2018/08/11 v1.3c Input encoding file
inpenc@prehook=toks14
inpenc@posthook=toks15
) (/usr/local/texlive/2019/texmf-dist/tex/latex/listings/listings.sty (/usr/loc
al/texlive/2019/texmf-dist/tex/latex/graphics/keyval.sty
Package: keyval 2014/10/28 v1.15 key=value parser (DPC)
KV@toks@=toks16
)
lst@mode=count88
lst@gtempboxa=box27
lst@token=toks17
lst@length=count89
lst@currlwidth=dimen103
lst@column=count90
lst@pos=count91
lst@lostspace=dimen104
lst@width=dimen105
lst@newlines=count92
lst@lineno=count93
lst@maxwidth=dimen106
(/usr/local/texlive/2019/texmf-dist/tex/latex/listings/lstmisc.sty
File: lstmisc.sty 2019/09/10 1.8c (Carsten Heinz)
c@lstnumber=count94
lst@skipnumbers=count95
lst@framebox=box28
) (/usr/local/texlive/2019/texmf-dist/tex/latex/listings/listings.cfg
File: listings.cfg 2019/09/10 1.8c listings configuration
))
Package: listings 2019/09/10 1.8c (Carsten Heinz)
(/compile/output.aux)
openout1 = `output.aux'.

! Argument of lst@temp has an extra }.
<inserted text> 
                par 
l.19 }
      
I've run across a `}' that doesn't seem to match anything.
For example, `defa#1{...}' and `a}' would produce
this error. If you simply proceed now, the `par' that
I've just inserted will cause me to report a runaway
argument that might be the root of the problem. But if
your `}' was spurious, just type `2' and it will go away.

Runaway argument?
But 
! Paragraph ended before lst@temp was complete.
<to be read again> 
                   par 
l.19 }
      
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.

! Missing $ inserted.
<inserted text> 
                $
l.20 
     
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.

)
Runaway argument?
{lstinline !But not { this!} par end {document} 
! File ended while scanning use of centerline .
<inserted text> 
                par 
<*> main.tex
            
I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.

! Emergency stop.
<*> main.tex
            
*** (job aborted, no legal end found)

One Answer

The code from the question:

fbox{% Error here
    lstinline!But % this does!
}

First fbox reads it argument. Category codes are not changed, thus the percent sign acts as comment char. The argument is: lstinline!But

Then lstinline is executed. It sees the first delimiter !, but the second is gone.

Best is to avoid verbatim stuff that wants to change category codes in the argument of other commands.


The second case:

centerline{lstinline!But not { this!}

end{document}

Again, centerline has one undelimited argument that starts with lstinline!But not { this!}parend{document}, but the argument requires that the curly braces with their usual category codes are matched properly. There are two pairs { this!} and {document}, but the closing curly brace for that matches the beginning curly brace centerline{ is missing. The category code change of lstinline cannot have an effect, because it is not yet executed.


In some (rare) cases, there is an environment form of a command that can be used instead. For example, sbox{mybox}{...} also reads its arguments as normal argument, thus lstinline can break here. But, environment lrbox works differently:

begin{lrbox}{mybox}lstinline!...!end{lrbox}

The contents of the box is not parsed as argument here. begin{lrbox} opens the box, then lstinline is executed and can change the category codes, and finally end{lrbox} closes the box.

Answered by Heiko Oberdiek on September 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP