如何强调列表中的字符“&”?

如何强调列表中的字符“&”?

如何listings在表格环境中使用包强调列分隔符?这是我的 MWE:

\documentclass{article}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{array,longtable,tabularx,ctable}
\usepackage{siunitx}
\usepackage{listings}
\lstset{
   language={[LaTeX]tex},
   basicstyle=\ttfamily,
   keywordstyle=\color{blue}\bfseries,
   stringstyle=\color{red},
   commentstyle=\color{green},
   columns=fullflexible,
   %
   morekeywords={tabular,toprule,midrule,bottomrule},
   otherkeywords={},
   emph={},
   emphstyle={\color{red}\bfseries}
}
\begin{document}
This is my tabular material:

\medskip

\begin{tabular}{lcl}
\toprule
2.3456   & 2.3456    & 0.1 \\
12,3456 & 12,3456  & 12,3456   \\
-9,8765  & -9,8765  & -9,8765   \\
\bottomrule
\end{tabular}

\medskip

And this is the code:

\medskip

\begin{lstlisting}
\begin{tabular}{lcl}
\toprule
2.3456   & 2.3456    & 0.1 \\
12,3456 & 12,3456  & 12,3456   \\
-9,8765  & -9,8765  & -9,8765   \\
\bottomrule
\end{tabular}
\end{lstlisting}

\end{document}

角色怎么能&有分离的强调风格?TYIA。

答案1

拥有

   otherkeywords={\&},
   emph={&},
   emphstyle={\color{red}\bfseries}

lstset这个例子中,但是listings手册警告不要这样做,IIUC:

最后一条提示:保持标识符列表不相交。切勿在“强调”列表中使用关键字,也不要将一个名称放在两个不同的列表中。即使您的源代码按预期突出显示,但如果您更改列表顺序或使用此软件包的下一个版本,也不能保证情况仍然如此。

\documentclass{article}
\usepackage{lmodern}
\usepackage{graphicx}
\usepackage{array,longtable,tabularx,ctable}
\usepackage{siunitx}
\usepackage{listings}
\lstset{
   language={[LaTeX]tex},
   basicstyle=\ttfamily,
   keywordstyle=\color{blue}\bfseries,
   stringstyle=\color{red},
   commentstyle=\color{green},
   columns=fullflexible,
   %
   morekeywords={,tabular,toprule,midrule,bottomrule},
   otherkeywords={\&},
   emph={&},
   emphstyle={\color{red}\bfseries}
}
\begin{document}
This is my tabular material:

\medskip

\begin{tabular}{lcl}
\toprule
2.3456   & 2.3456    & 0.1 \\
12,3456 & 12,3456  & 12,3456   \\
-9,8765  & -9,8765  & -9,8765   \\
\bottomrule
\end{tabular}

\medskip

And this is the code:

\medskip

\begin{lstlisting}
\begin{tabular}{lcl}
\toprule
2.3456   & 2.3456    & 0.1 \\
12,3456 & 12,3456  & 12,3456   \\
-9,8765  & -9,8765  & -9,8765   \\
\bottomrule
\end{tabular}
\end{lstlisting}

\end{document}

在此处输入图片描述

答案2

其他解决方案解决了您的具体问题,但我强烈建议您考虑使用包裹showexpl它利用了listings包。这消除了 LaTeX 代码的重复,因此不容易出错。

在此处输入图片描述

笔记:

代码:

\documentclass{article}
\usepackage{ctable}
\usepackage{xcolor}
\usepackage{showexpl}% includes listings

% https://tex.stackexchange.com/questions/17646/fixed-width-font-with-ltxexample-environment
\sbox0{\small\ttfamily A}
\edef\mybasewidth{\the\wd0 }

\lstdefinestyle{demoLatexStyle}{
    basicstyle=\ttfamily,% control font of code
    preset=\small,% adjust font size of output
    numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt,
    frame=tlbr,
    rframe={},% no frame around the output
    pos=r,% output on right
    backgroundcolor=\color{yellow!30},
    width=0.45\linewidth,
    keywordstyle=\color{blue}\bfseries,
    stringstyle=\color{red},
    commentstyle=\color{green},
    columns=fixed,basewidth=\mybasewidth,
    morekeywords={,tabular,toprule,midrule,bottomrule},
    otherkeywords={\&},
    emph={&},
    emphstyle={\color{red}\bfseries}
}
\lstloadlanguages{[LaTeX]TeX}

\begin{document}
\noindent
This is my tabular material and the associated code:
\medskip
\begin{LTXexample}[style=demoLatexStyle]
\begin{tabular}{lcl}
\toprule
2.3456   & 2.3456   & 0.1     \\
12,3456  & 12,3456  & 12,3456 \\
-9,8765  & -9,8765  & -9,8765 \\
\bottomrule
\end{tabular}
\end{LTXexample}
\end{document}

相关内容