16
\documentclass[14pt]{extarticle}
\usepackage{xcolor}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\usepackage{listings}
\lstloadlanguages{[LaTeX]TeX}
\lstdefinestyle{listLaTeX}{
  language={[LaTeX]TeX},
  extendedchars=true,
  belowcaptionskip=5pt,
  xleftmargin=0.5cm,
  numbers=left,
  numberstyle=\scriptsize\ttfamily\bf,
  frame=single,
  breaklines=true,
  breakatwhitespace=true,
  breakindent=0pt,
  basicstyle=\small\ttfamily,
  keywordstyle=\color{deepblue}\ttfamily,
  identifierstyle=\color{deepred}\ttfamily,
  commentstyle=\color{deepgreen}\ttfamily,
  morekeywords={},
  emphstyle=\color{red},
  emph={},
  columns=fullflexible,
  showstringspaces=false
}
\begin{document}
Test -- \lstinline[style=listLaTeX]!\Huge!!

\begin{tabular}{|p{0.29\linewidth}|p{0.29\linewidth}|p{0.29\linewidth}|}
\hline
1&2&3\\
\lstinline[style=listLaTeX]!\Huge!&2&3\\%This line has an error!!!!
\hline
\end{tabular}
\end{document}
1

3 Answers 3

19

The answer by egreg didn't work for me, so her is an example how I do it:

    \documentclass[varwidth]{standalone}
    \usepackage {listings}
    \usepackage {xcolor}

    % Color scheme: original matlab
    \definecolor{src_none}{HTML}      {555555} % 
    \definecolor{src_comment}{HTML}   {008000} % gruen

    \lstset{language            = Matlab,
            basicstyle          = \color{src_none}\ttfamily\footnotesize,
            stringstyle         = \color{src_comment}}

    \begin{document}
      lorem \lstinline{foo = 'bar';} ipsum.\\
      \begin{tabular}{ll}
        11                        & 12\\
        \lstinline[]$bar = 'foo';$  & 22\\
      \end{tabular}

    \end{document}

notice that I've used '$$' instead of brackets '{}' it results in:

enter image description here

1
  • Using other delimiters (like !!) works too.
    – vonbrand
    Jun 21, 2021 at 23:44
13

This has to do with the "look forward" techniques that \lstinline has to use for finding the end of its argument; with your input it finds the end of the cell, which is an implicit token used internally by TeX and is not legal in user input.

Protecting the \lstinline with braces solves the issue, because the look forward will find the closing brace instead of the illegal token \endtemplate:

\begin{tabular}{|p{0.29\linewidth}|p{0.29\linewidth}|p{0.29\linewidth}|}
\hline
1 & 2 & 3\\
{\lstinline[style=listLaTeX]!\Huge!} & 2 & 3 \\
\hline
\end{tabular}
1
  • Uh, oops, yeah I looked to quickly while trying to solve a similar problem, disregard my comment
    – BrtH
    Jul 27 at 8:50
1

Instead of using \lstinline{xxx}, try \lstinline|xxx| or \lstinline$xxx$.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .