Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 8 down vote accepted

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

share|improve this answer
    
Thanks a lot. I'll keep in mind your hint. –  agodemar Dec 5 '11 at 16:51

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

% http://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}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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