3

I would like to save chosen listings style to a variable \mlstyle and use it later in some commands or definitions.

However, while:

\lstinputlisting[style=Matlab-editor]{a.mat} 

a version that I expected to use:

\newcommand{\mlstyle}{Matlab-editor} 
\lstinputlisting[style=\mlstyle]{a.mat}

does not.

Package Listings Error:
style Matlab-editor undefined.
\lstinputlisting[style=\mlstyle]{a.mat}

\documentclass{article}

\usepackage{filecontents} % only need to provide the file a.mat

\usepackage{listing}
\usepackage{matlab-prettifier}
\newcommand{\mlstyle}{Matlab-editor}%{Matlab-bw}%{Matlab-Pyglike}

\begin{filecontents*}{a.mat}
    a = 1;
\end{filecontents*}

\begin{document}

\lstinputlisting[style=Matlab-editor]{a.mat} % WORKS

%\lstinputlisting[style=\mlstyle]{a.mat} % DOES NOT WORK

\end{document}

Are there any workarounds?

  • Most key-value interfaces do not expandthe values given as options – user31729 Feb 26 '18 at 22:36
  • 'Simple' solution: \expandafter\lstinputlisting\expandafter[\mlstyle]{a.mat} where \mlstyle is style=Matlab-editor – user31729 Feb 26 '18 at 22:39
2

When listings parses options, it generally does no expansion, but we can coerce it to do it for style:

\begin{filecontents*}{\jobname.mat}
    a = 1;
\end{filecontents*}

\documentclass{article}

\usepackage{listing}
\usepackage{matlab-prettifier}

\makeatletter
\AtBeginDocument{%
  \lst@Key{style}\relax{%
    \begingroup\edef\x{\endgroup
      \noexpand\lst@LAS{style}{sty}{[]{#1}}%
    }\x\lst@NoAlias\lststylefiles
    \lsthk@SetStyle{}%
  }%
}
\makeatother

\newcommand{\mlstyle}{Matlab-editor}

\begin{document}

\lstinputlisting[style=Matlab-editor]{\jobname.mat}

\lstinputlisting[style=\mlstyle]{\jobname.mat}

\end{document}

enter image description here

2

It works if you use lowercase letters in the style name:

\documentclass{article}

\usepackage{filecontents} % only need to provide the file a.mat

\usepackage{listings}
\usepackage{matlab-prettifier}
\newcommand{\mlstyle}{matlab-editor}%

\begin{filecontents*}{a.mat}
    a = 1;
\end{filecontents*}

\begin{document}

\lstinputlisting[style=\mlstyle]{a.mat} % 

\end{document}
2

It's an expansion issue. I suggest making \mlstyle into a macro. For example, if you define

\newcommand{\mlstyle}[2][editor]{\lstinputlisting[style=Matlab-#1]{#2}}

then you can use:

\mlstyle{a.mat} % DOES WORK with style = Matlab-editor
\mlstyle[bw]{a.mat} % DOES WORK with style = Matlab-bw
\mlstyle[Pyglike]{a.mat} % DOES WORK with style = Matlab-Pyglike

Here's the full code:

\documentclass{article}

\usepackage{filecontents} % only need to provide the file a.mat

\usepackage{listing}
\usepackage{matlab-prettifier}
% usage: \mlstyle[matlab style]{filename}
% where the default matlab style is Matlab-editor
% and \mlstyle[X]{filename} uses style=Matlab-X
\newcommand{\mlstyle}[2][editor]{\lstinputlisting[style=Matlab-#1]{#2}}

\begin{filecontents*}{a.mat}
    a = 1;
\end{filecontents*}

\begin{document}

\lstinputlisting[style=Matlab-editor]{a.mat} % WORKS

\mlstyle{a.mat} % DOES WORK with style = Matlab-editor
\mlstyle[bw]{a.mat} % DOES WORK with style = Matlab-bw
\mlstyle[Pyglike]{a.mat} % DOES WORK with style = Matlab-Pyglike

\end{document}

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.