5

This is my first question so feel free to correct it in any way.

I am struggling with the following problem: using an environment in a \newcommand.

I want to define the command \describe this way: (It takes a bash command name (#1), an example using it (#2) and a description (#3)).

\newcommand{\describe}[3]{
\subsection*{#1}
\begin{lstlisting}
#2
\end{lstlisting}
{\colorbox{gray}{#1}\ #3}
}

The only thing I am getting is a

"no legal \end found"

error.

So my question is: Is there any way to use an environment inside a new command?

4ster
  • 93
  • \lstinline is what you want. – Manuel Apr 02 '16 at 12:08
  • In addition to Manuels correct answer, in latex there are certain constructions that cannot be used inside a macro definition. Verbatim like constructions like listings are among these. – daleif Apr 02 '16 at 12:13
  • @Manuel this command stops after the end of the code snippet. I want the look of the environment (takes a full line) – 4ster Apr 02 '16 at 12:14
  • @daleif how to work around this problem? – 4ster Apr 02 '16 at 12:15
  • 1
    There is no work around, if you want the look of the environment, use the env directly, it will also give your code a much better structure. – daleif Apr 02 '16 at 12:26
  • @daleif actually I am building a bigger command taking 3 variables and building them together, where the code snippet is positioned in the middle between a subsection and a simple text. Using the environment would force me to brake up the whole construction... – 4ster Apr 02 '16 at 12:33
  • Well, perhaps you should show us what you're trying in a document, not just a fragment. We might give you some guidance –  Apr 02 '16 at 12:34
  • verbatim commands like \verb verbatim and listings can not be used inside the argument of another command. – David Carlisle Apr 02 '16 at 12:35
  • @ChristianHupfer I added the full construction, perhaps it's clearer this way. Thanks for all the comments so far! – 4ster Apr 02 '16 at 12:58
  • @4ster: I've got another idea, since you're using a colorbox as well –  Apr 02 '16 at 13:04

1 Answers1

1

A \newcommand with a verbatim content environment does not work together (without much ado) and my approach will not work always.

Verbatim environment such as lstlisting need a definite end point where the verbatim content stops and the usual LaTeX (i.e. the expansion) should continue. this can be done with \scantokens{...} to provide the \end{tcblisting} here as the end token.

I've decided to use tcolorbox and its listings options, since a colorbox is apparently involved.

There are many configuration options of tcolorbox, I've restricted to the main feature here (also the usage of specialized listings environments in conjunction with xparse).

The xparse package allows to provide verbatim arguments v which are just taken as status quo.

\documentclass{article}


\usepackage{xcolor}
\usepackage{xparse}
\usepackage[most]{tcolorbox}

\begin{document}





\NewDocumentCommand{\describe}{O{}+m+v+m}{%
  \scantokens{%
    \begin{tcblisting}{enhanced,
        before={\subsection*{#2}},
        title={#4},
        listing only,
        colback={white!80!black},
        sharp corners,
        colbacktitle={white!60!black},
        boxrule=1pt,
        left=5pt,
        #1}
      #3
    \end{tcblisting}
  }%
}


\describe{My command}{ls -ltr}{Foo}

\describe[colback=green!40!white]{Another command}{pdflatex thisniceexample}{Do you like what you see?}

\end{document}

enter image description here

  • so there's basically no workaround that doesn't involve the usage of xparse? I'm just a bit confused that there's no simple LaTeX solution to such a simple problem - the question that has been marked as a duplicate by @egreg doesn't help neither... – 4ster Apr 02 '16 at 15:33
  • @4ster: Perhaps you can omit the v argument and xparse, but not \scantokens. And it's not a simple problem, but you have to prevent LaTe interpreting your bash code as LaTeX –  Apr 03 '16 at 10:30