3

How do I emphasize with the listings package the column separators in a tabular environment? This is my 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}

How can the character & have a separated emphasizing style? TYIA.

8

Having

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

in the lstset works for this example, but the listings manual warns against it, IIUC:

One final hint: Keep the lists of identifiers disjoint. Never use a keyword in an ‘emphasize’ list or one name in two different lists. Even if your source code is highlighted as expected, there is no guarantee that it is still the case if you change the order of your listings or if you use the next release of this package.

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

enter image description here

|improve this answer|||||
3

The other solution addresses your specific question, but I would highly recommend that you consider using the showexpl pacakge which makes use of the listings package. This eliminates the duplication of the LaTeX code, so it is not as error prone.

enter image description here

Note:

Code:

\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}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.