2

I like to have consistent documents and I like to make some decisions on formatting after I finished writing the document. I use my own placeholder LaTeX commands to wrap some words or expressions. For example, I'd like to use a command called \Shortcut to wrap \lstinline with a suitable style parameter. (whether I'll use \lstinline in the end does not matter, I may change that to \textcolor or whatever). Until now my approach simply worked. But it does not work in a tabular environment. How can I make it work? Here is my MWE:

\documentclass{book}

\usepackage{tabularx}
\usepackage{listings}
\usepackage{xcolor}

\lstdefinestyle{inlcode}{
    language=C,
    identifierstyle=\color{blue!75!black}
}
\newcommand\Shortcut{\lstinline[style=inlcode]}

\begin{document}
  \Shortcut{test}            % <--- works

  \begin{tabular}[t]{cc}
    1 & 2 \\
    \Shortcut{one} & two   % <--- doesn't work
  \end{tabular}
\end{document}
1

I have changed the definition of your custom command to allow for the use of an argument.

The general syntax of \newcommand is the following: \newcommand{<name>}[<number of arguments>]{<code>}

\documentclass{book}

\usepackage{tabularx}
\usepackage{listings}
\usepackage{xcolor}

\lstdefinestyle{inlcode}{
    language=C,
    identifierstyle=\color{blue!75!black}
}
\newcommand{\Shortcut}[1]{\lstinline[style=inlcode]{#1}}

\begin{document}
  \Shortcut{test}

  \begin{tabular}[t]{cc}
    1 & 2 \\
    \Shortcut{one} & two
  \end{tabular}

\end{document}
| improve this answer | |
  • Could you add an explanation of why the version without an explicit argument doesn't work in tabulars? – siracusa Jun 27 '19 at 22:57
  • 2
    Lstinline is a verbatim like command. By using an argument you freeze the catcodes and actually break it. – Ulrike Fischer Jun 28 '19 at 6:00

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.