2

I have been following this topic about using \NewDocuentCommand with lstlisting and the accepted answer is nice except it is too big and it doesn't quite work for me.

I came up with this example to test xparse:

\documentclass{article}
\usepackage{xparse}
\usepackage{listings}

\ExplSyntaxOn
\NewDocumentCommand{\code}{m +v}{
    \begin{lstlisting}[style=#1,numbers=none,xleftmargin=0.35cm,framesep=0mm]
        #2
    \end{lstlisting}
}
\ExplSyntaxOff
   
\begin{document}

\code{C}{
int x = 200;
unsigned int y = 0;
}

\end{document}

Like stated in the CTAN xparse documentation I first used m as a normal parameter and then +v as a multiline verbatim parameter, but it fails. It looks like I am still missing something...

11
  • 2
    You cannot wrap verbatim into macros. This doesn't change with LaTeX3. Jun 21, 2017 at 7:34
  • Use \lstnewenvironment instead. Jun 21, 2017 at 7:37
  • @Henri Menke I need to create a command and not environment. In the example I provided they did it somehow...
    – 71GA
    Jun 21, 2017 at 7:53
  • @71GA you didn't provide an example: I guess you forgot to add the link…
    – cgnieder
    Jun 21, 2017 at 7:57
  • 1
    @71GA »I need to create a command...« Wrong! You want to create a command but what you need and should use is an environment. Jun 21, 2017 at 8:14

1 Answer 1

5

You can just use Bruno's solution from the linked question (pretty smart I think).

\documentclass{article}
\usepackage{listings}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\code}{m +v}{
  \newlinechar=\endlinechar
  \exp_args:Nx \scantokens
  {
    \string\begin{lstlisting}[\unexpanded{language=#1,numbers=none,xleftmargin=0.35cm,framesep=0mm}]
      #2
    \string\end{lstlisting}
  }
}
\ExplSyntaxOff

\begin{document}

\code{C}{
int x = 200;
unsigned int y = 0;
}

\end{document}

enter image description here


But please, just use a new environment instead.

\documentclass{article}
\pagestyle{empty}
\usepackage{listings}

\lstnewenvironment{code}[1]{\lstset{language=#1,numbers=none,xleftmargin=0.35cm,framesep=0mm}}{}

\begin{document}

\begin{code}{C}
int x = 200;
unsigned int y = 0;
\end{code}

\end{document}

Output is similar.

11
  • Thank you for the answer. One more thing is bugging me. In either of the cases you provided, code is moved one tab to the right if I move source code \begin{code}...\end{code} one tab right. So source code position affects the compiled code position. Any idea on how to solve this?
    – 71GA
    Jun 21, 2017 at 8:47
  • @71GA See the gobble option in the documentation. Jun 21, 2017 at 9:22
  • @ Henri Menke In the first solution, if the content of the second argument(+v) locates at the same line where \code{ is, this part of content will not be typeset. Why and how to deal with that?
    – lyl
    Jul 26, 2022 at 13:26
  • @lyl lstlisting environment ignores the content on its first line as well, so no surprise here. To deal with it insert an explicit ^^J at the end of the lines.
    – user202729
    Jul 27, 2022 at 4:29
  • @user202729 I tried with the above example of the first solution like this ... unsigned int y = 0; ^^J, but only get ... unsigned int y = 0; ^^J
    – lyl
    Jul 27, 2022 at 4:40

You must log in to answer this question.

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