3

lstlisting has an option called frame=

Mathematica graphics

But when I use the frame=none in \lstdefinestyle and put the lstlisting command inside tabular or longtable I get an error

  Forbidden control sequence

It seems like a key=value clash of some sort. Here is a MWE

\documentclass[11pt]{article}
\usepackage{listings}
\usepackage{matlab-prettifier}
\lstdefinestyle{matlab}{style=Matlab-editor,
  basicstyle=\ttfamily\normalsize,
  escapechar         = `,
  mlshowsectionrules = true,frame=none}

\begin{document}    
\begin{tabular}{p{0.5\textwidth} }
\begin{lstlisting}[style=matlab]
clear all;
the_zeros = [-1 -2];
\end{lstlisting}
\end{tabular}
\end{document}

Compile

pdflatex foo2.tex
This is pdfTeX, Version 3.14159265-2.6-1.40.16 (TeX Live 2015) (preloaded format=pdflatex)
 restricted \write18 enabled.
entering extended mode
(./foo2.tex
LaTeX2e <2015/10/01> patch level 2
Babel <3.9m> and hyphenation patterns for 79 languages loaded.
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/size11.clo))
(/usr/local/texlive/2015/texmf-dist/tex/latex/matlab-prettifier/matlab-prettifi
er.sty (/usr/local/texlive/2015/texmf-dist/tex/latex/base/textcomp.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/base/ts1enc.def))
(/usr/local/texlive/2015/texmf-dist/tex/latex/xcolor/xcolor.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/latexconfig/color.cfg)
(/usr/local/texlive/2015/texmf-dist/tex/latex/pdftex-def/pdftex.def
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/infwarerr.sty)
(/usr/local/texlive/2015/texmf-dist/tex/generic/oberdiek/ltxcmds.sty)))
(/usr/local/texlive/2015/texmf-dist/tex/latex/listings/listings.sty
(/usr/local/texlive/2015/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/listings/lstmisc.sty)
(/usr/local/texlive/2015/texmf-dist/tex/latex/listings/listings.cfg)))
(./foo2.aux) (/usr/local/texlive/2015/texmf-dist/tex/latex/base/ts1cmr.fd)
(/usr/local/texlive/2015/texmf-dist/tex/context/base/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
)
Runaway definition?
#1\\none\@endpbox \hskip \tabcolsep 
! Forbidden control sequence found while scanning definition of \lst@temp.
<inserted text> 
                }
l.14 \begin{lstlisting}[style=matlab]

? 

When I remove the frame=none from options, Latex is happy and no error. This option is valid. If I remove the code from inside the tabular, it also compiles ok with the option there:

\documentclass[11pt]{article}
\usepackage{matlab-prettifier}
\lstdefinestyle{matlab}{style=Matlab-editor,
  basicstyle=\ttfamily\normalsize,
  escapechar         = `,
  mlshowsectionrules = true,frame=none}

\begin{document}        
\begin{lstlisting}[style=matlab]
clear all;
the_zeros = [-1 -2];
\end{lstlisting}
\end{document}

no error.

question is: Why one gets an error when using frame=none inside tabular and longtable but not outside? I need to use the option frame and also put the listing inside a table.

I also found that it works if I use lstset, like this

\documentclass[11pt]{article}
\usepackage{listings}
\usepackage{matlab-prettifier}
\lstset{style=Matlab-editor,
  basicstyle=\ttfamily\normalsize,
  escapechar         = `,
  mlshowsectionrules = true,frame=none}

\begin{document}    
\begin{tabular}{p{0.5\textwidth} }
    \begin{lstlisting}
    clear all;
    the_zeros = [-1 -2];
    \end{lstlisting}
\end{tabular}
\end{document}

But I need to use \lstdefinestyle to define the styles, since I have other languages I need to use listings for, and I do not want to set one lstset globally. That is why I am loading listings as well. I have other \lstdefinestyle for other languages, which I did not show up. But only the option frame= is the one that gives an error.

TL 2015

1 Answer 1

4

looks like a bug but as usual with these things, an extra {} helps

\documentclass[11pt]{article}
\usepackage{listings}
\usepackage{matlab-prettifier}
\lstdefinestyle{matlab}{style=Matlab-editor,
  basicstyle=\ttfamily\normalsize,
  escapechar         = `,
  mlshowsectionrules = true,
frame=none
}

\begin{document}

\begin{tabular}{p{0.5\textwidth} }
{\begin{lstlisting}[style=matlab]
clear all;
the_zeros = [-1 -2];
\end{lstlisting}}
\end{tabular}
\end{document}

The bug in listings is here

\def\lstKV@SwitchCases#1#2#3{%
    \def\lst@temp##1\\#1&##2\\##3##4\@nil{%
        \ifx\@empty##3%
            #3%
        \else
            ##2%
        \fi
    }%
    \lst@temp\\#2\\#1&\\\@empty\@nil}

using & as an "arbitrary separator token" is a brave choice, any token could have been used there, but changing it would require changes in several places within the package where this is used.

You could report it to the package maintainer.

You must log in to answer this question.

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