3

I am trying to add comments to the left of a listings environment by following the suggestions shown here: https://zuttobenkyou.wordpress.com/2010/12/05/latex-saner-source-code-listings-no-starch-press-style/ using the command \vbox{\llap{*\hskip 1em}} inside of an escaped sequence.

But when I put the command inside of a macro it shifts the line over, while if I directly add the command there is no shift. Can anybody explain why this is the case? A MWE is shown below.

For background, listings allows you to revert back to arbitrary LaTeX code inside of a verbatim environment if you place the code inside of an escape sequence, which in this case is defined as starting with (*@ and ending with @*). The llap command creates a box of width zero; the argument is then placed just to the left of this zero-width box (and hence will overlap whatever lies to the left). I'm not sure what the point of the vbox command is.

\documentclass{article}

\usepackage{listings}

\newcommand*{\lnote}{
  \vbox{\llap{*\hskip 1em}}
}

\begin{document}

\begin{lstlisting}[escapeinside={(*@}{@*)}, frame=single]
(*@\vbox{\llap{*\hskip 1em}}@*)echo hello
(*@\lnote@*)echo goodbye
\end{lstlisting}

\end{document}

When the document is compiled this is what it looks like. The first line works as expected, and the second line is incurring an undesired horizontal shift.

enter image description here

3

your lnote command is adding space tokens. I commented them out of the definition here.

\documentclass{article}

\usepackage{listings}

\newcommand*{\lnote}{%
  \vbox{\llap{*\hskip 1em}}%
}

\begin{document}

\begin{lstlisting}[escapeinside={(*@}{@*)}, frame=single]
(*@\vbox{\llap{*\hskip 1em}}@*)echo hello
(*@\lnote@*)echo goodbye
\end{lstlisting}

\end{document}

why a vbox here? Seems a very non-latex construct.

  • 1
    Yep, that did the trick, thanks! I agree, I don't see the point of the vbox here, but I wanted to leave it in just in case it had something to do with the issue. – dpritch Mar 13 '17 at 1:17

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.