带有特殊字符的 lstinline 破坏了某些环境(失控参数)

带有特殊字符的 lstinline 破坏了某些环境(失控参数)

我试图用某些环境(如\fbox或 )封闭内联列表\centerline。下面的 MWE 说明了如何\lstinline正常转义%{字符,但在上述环境中我收到错误。我该如何解决?

有趣的是,其他类似的环境tcolorbox可以正常工作。这里有什么不同?

\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}

日志(略微修剪):

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, `\def\a#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)

答案1

问题中的代码:

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

首先\fbox读取它的参数。类别代码没有改变,因此百分号充当注释字符。该参数是:\lstinline!But

然后\lstinline执行。它看到第一个分隔符!,但第二个分隔符消失了。

最好是避免逐字逐句地改变其他命令参数中的类别代码。


第二种情况:

\centerline{\lstinline!But not { this!}

\end{document}

同样,\centerline有一个以 开头的未限定参数\lstinline!But not { this!}\par\end{document},但该参数要求花括号与其通常的类别代码正确匹配。有两对{ this!}和,但缺少{document}与开头花括号匹配的 的结束花括号。 的类别代码更改无法产生效果,因为它尚未执行。\centerline{\lstinline


在某些(罕见)情况下,可以使用命令的环境形式。例如,\sbox{\mybox}{...}也将其参数读取为正常参数,因此\lstinline可能会在此处中断。但是,环境的lrbox 工作方式不同:

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

这里不将盒子的内容作为参数进行解析。\begin{lrbox}打开盒子,然后\lstinline执行并可以更改类别代码,最后\end{lrbox}关闭盒子。

相关内容