我有一个tcolorbox
+listings
驱动的环境,例如终端会话 - 我想在用户输入前加上前缀,以将其(通过颜色/重量/等)与计算机输出区分开来。
用 定义分隔\lstset
符很好,但我不能在 中使用相同的定义listing options
。\newtcblisting
相反,TeX 突然决定它需要输入一个文件才能自我感觉良好。
平均能量损失
\documentclass{article}
\usepackage{listings}
\newif\ifbroken
\brokentrue % toggle me!
\ifbroken
\usepackage[most]{tcolorbox}
\newtcblisting{exampleoutput}{
listing options={
moredelim=[il][\bfseries]{>>},
}
}
\else % works just fine
\lstset{moredelim=[il][\bfseries]{>>}}
\fi
\begin{document}
\ifbroken
\begin{exampleoutput}
>>hello
\end{exampleoutput}
\else
\begin{lstlisting}
>>hello
\end{lstlisting}
\fi
\end{document}
ERROR: Package Listings Error: File `[(.tex)' not found.
--- TeX said ---
Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)
Enter file name:
./mwe.tex:27: Emergency stop.
<read *>
l.27 \end{exampleoutput}^^M
--- HELP ---
From the .log file...
*** (cannot \read from terminal in nonstop modes)
答案1
必须将within选项括起来,以[...]
防止选项处理的脆弱性(即如何将此选项传输到环境及其可选参数。)moredelim
{...}
tcolorbox
listings
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\newif\ifbroken
\brokentrue % toggle me!
\ifbroken
\usepackage[most]{tcolorbox}
\newtcblisting{exampleoutput}{
listing options={{
moredelim=[il][\bfseries]{>>}},
}
}
\else % works just fine
\lstset{moredelim=[il][\bfseries]{>>}}
\fi
\begin{document}
\ifbroken
\begin{exampleoutput}
>>hello
\end{exampleoutput}
\else
\begin{lstlisting}
>>hello
\end{lstlisting}
\fi
\end{document}