4

In the following example I would like to shortcut the \lstinline with a shorter command such as \cd (code). Unfortunately it does not work with backslashes:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstset{language=c}
\newcommand{\cd}[1]{\lstinline{#1}}

\begin{document}

\lstinline{\ } % This works
\cd{\ } % This doesn't work

\end{document}

In the above example, the \lstinline{\ } works, but with \cd the backslash does not work. How to fix this?

8

Just avoid passing the argument and define

\newcommand{\cd}{\lstinline}
  • Should I need the braces around {\cd} ? – nowox Aug 12 at 15:18
  • @nowox They're not strictly necessary but are considered good LaTeX style. – campa Aug 12 at 20:03

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.