3

Problem description

For a tutorial on interactive Unix shell usage, I am using \usepackage{listings} with the breaklines=true option to automatically break long shell commands across multiple lines.

On an interactive shell, line breaks are inserted into commands by terminating the line with \ and hitting <RETURN>, e.g.:

ls \ <RETURN>
-l

To illustrate this with the listings package, I would like to configure its prebreak option sich as to terminate long lines with \, followed by the \return macro from the menukeys package (which draws a symbol for the return key).

However, although I can get the prebreak option to work with other symbols (e.g., \leftarrow), using \return breaks compilation. How can I get it to work?

Mimimal working example (MWE)

\documentclass{article}

\usepackage{menukeys}% Provides \return (return key symbol)

\usepackage{listings}%

\lstdefinestyle{shell}{
  breaklines=true,%
  %
  % Backslash only works, but I want to append \return from \usepackage{menukeys} to it
  prebreak=\char`\\,%
  %
  % This works, but uses the wrong symbol.
  %prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\leftarrow}},
  % Source:
  % http://www.bollchen.de/blog/2011/04/good-looking-line-breaks-with-the-listings-package/
  %
  % TODO: Various strategies for using \return all break compilation
  %prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\return}},%
  %prebreak=\raisebox{0ex}[0ex][0ex]{\return},%
  %prebreak={\return},%
}

\begin{document}

\begin{lstlisting}[style=shell]
  sleep 1; sleep 2; sleep 3; sleep 4; sleep 5; sleep 6; sleep 7; sleep 8; sleep 9; sleep 10
\end{lstlisting}

\end{document}
Florian H.
  • 691
  • 3
  • 9

1 Answers1

5

Your code (without return) doesn't work for me: your prebreak results in a double quote. Using \textbackslash instead works.

Regarding \return: I suggest do typeset the return symbol into a box, save it, and later only use the pre-typeset symbol.

\usepackage{menukeys}% Provides \return (return key symbol)
\usepackage{listings}%
\newsavebox\RET
\sbox\RET{\return}
\lstdefinestyle{shell}{
  breaklines=true,%
  prebreak=\textbackslash\usebox\RET
}

enter image description here

\documentclass{article}

\usepackage{menukeys}% Provides \return (return key symbol)
\usepackage{listings}%
\newsavebox\RET
\sbox\RET{\return}
\lstdefinestyle{shell}{
  breaklines=true,%
  prebreak=\textbackslash\usebox\RET
}

\begin{document}
\begin{lstlisting}[style=shell]
  sleep 1; sleep 2; sleep 3; sleep 4; sleep 5; sleep 6; sleep 7; sleep 8; sleep 9; sleep 10
\end{lstlisting}

\end{document}
gernot
  • 49,614
  • Great answer, thanks! Strange that my MWE does not work for you... I just saved the MWE as-is to a file mwe.tex and compiled it successfully via pdflatex mwe (Debian, TeXLive 2016) – Florian H. Mar 04 '17 at 17:02
  • Also worth noting that by comparison to the suggested solution, I inserted a horizontal space between the backslash and return. I did this by changing \sbox\RET{\return} to \sbox\RET{\ \return}, since adding the space to the prebreak option broke compilation. – Florian H. Mar 04 '17 at 17:04