我正在排版一些具有这种外观的系统的示例:
In: 2+2
Out: 4
我想强调一下“In:”和“Out”:这有效:
\begin{lstlisting}[basicstyle=\ttfamily,emph={In,Out},emphstyle=\bfseries]
In: 2+2
Out: 4
\end{lstlisting}
但是这个:
\begin{lstlisting}[basicstyle=\ttfamily,emph={In:,Out:},emphstyle=\bfseries]
In: 2+2
Out: 4
\end{lstlisting}
我想在强调部分包含冒号,但没有。我尝试过alsoletter={:}
在选项中包含冒号,但没有效果。
有没有简单的方法可以实现这一点?
请注意,我实际上将 listings 用作tcolorbox
包中定义的新环境的一部分。因此:
\newenvironment{pyth}{%
\tcblisting{listing only,colback=vlgray,colframe=vlgray,enhanced,
overlay={\node[draw,fill=black,xshift=-10pt,yshift=4pt,left,text=white,
anchor=east,font=\footnotesize\bfseries]
at (frame.south east) {Python};},%
listing options={alsoletter={:},emph={In:,Out:},emphstyle=\bfseries,
basicstyle=\small\ttfamily,breaklines=true,language=Python},}}
{\endtcblisting}
然后在我的文字中:
\begin{pyth}
In: 2+2
Out: 4
\end{pyth}
在这种特殊的环境中alsoletter
似乎没有效果。
答案1
alsoletter={:}
确实有效:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{beramono}
\begin{document}
\begin{lstlisting}[basicstyle=\ttfamily,alsoletter={:},emph={In:,Out:},emphstyle=\bfseries]
In: 2+2
Out: 4
\end{lstlisting}
\end{document}
问题编辑后更新:
首先指定语言:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{beramono}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\colorlet{vlgray}{gray}% just for the example, since the original definition was not included
\newenvironment{pyth}
{%
\tcblisting{
listing only,
colback=vlgray,
colframe=vlgray,
enhanced,
overlay={
\node[
draw,
fill=black,
xshift=-10pt,
yshift=4pt,
left,
text=white,
anchor=east,
font=\footnotesize\bfseries
]
at (frame.south east) {Python};
},%
listing options={
language=Python,
alsoletter={:},
emph={In:,Out:},
emphstyle=\bfseries,
basicstyle=\small\ttfamily,
breaklines=true,
},
}
}
{\endtcblisting}
\begin{document}
\begin{pyth}
In: 2+2
Out: 4
\end{pyth}
\end{document}
我beramono
在示例中使用了该包,只是为了获得等宽粗体字体。我将其用作\colorlet{vlgray}{gray}
示例,因为原始定义未包含在问题中。