3

Following this answer, I've tried to embed a listing with long lines into a beamer frame.

I'd like to have a nice red arrow at the line break (as show here).

The parameters prebreak and postbreak give an error while compiling it. As a matter of fact, it's the \textcolor that gives the error.

Here is the adapted MWE :

\documentclass[xcolor={svgnames,usenames,dvipsnames,table}]{beamer}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage{listings}

\lstset{%
    xleftmargin=3.4pt,
    xrightmargin=3.4pt,
    %keepspaces=true,
    columns=fullflexible,   % Rasterize the columns
    frame=single,
    basicstyle=\ttfamily\small,
    inputencoding=utf8,
    extendedchars=\true,
    backgroundcolor=\color{black!10},
    breaklines=true,
% This works
    postbreak=\mbox{$\hookrightarrow$\space},
% This does not work
    postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}


\begin{document}

\begin{frame}[fragile]{Test}
\begin{lstlisting}
This is a long line. This is a long line. This is a long line. This is a long line. This is a long line.
This is another long line. This is another long line. This is another long line. This is another long line.
\end{lstlisting}
\end{frame}
\end{document}

Here is the output of the code, without the \textcolor obviously.

MWE output

If I uncomment the postbreak line, I get this error :

! Missing number, treated as zero.
<to be read again> 
                   \kern 
l.3 This 
         is a long line. This is a long line. This is a long line. This is a...
A number should have been here; I inserted `0'.
(If you can't figure out why I needed to see a number,
look up `weird error' in the index to The TeXbook.)

Any ideas on why does the textcolor makes things crash ?

1
  • 3
    It is probably a good idea to leave that postbreak line uncommented in the MWE. Then it would fail without the user having to do anything. Here we have to read the MWE to know that it only fails if you remove a %
    – daleif
    Commented Oct 27, 2020 at 8:39

1 Answer 1

0

As was already suggested in other answers, you should place the arrow inside a box, which will help in the fragile environment:

\documentclass[xcolor={svgnames,usenames,dvipsnames,table}]{beamer}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage{amssymb}
\usepackage{listings}

\newsavebox{\mypostbreak}
\savebox{\mypostbreak}{\mbox{\textcolor{red}{$\hookrightarrow$}\space}}
\lstset{%
    xleftmargin=3.4pt,
    xrightmargin=3.4pt,
    %keepspaces=true,
    columns=fullflexible,   % Rasterize the columns
    frame=single,
    basicstyle=\ttfamily\small,
    inputencoding=utf8,
    extendedchars=\true,
    backgroundcolor=\color{black!10},
    breaklines=true,
% This works
    % postbreak=\mbox{$\hookrightarrow$\space},
% This now also works
    postbreak=\usebox{\mypostbreak},
}

\begin{document}

\begin{frame}[fragile]{Test}
\begin{lstlisting}
This is a long line. This is a long line. This is a long line. This is a long line. This is a long line.
This is another long line. This is another long line. This is another long line. This is another long line.
\end{lstlisting}
\end{frame}
\end{document}

enter image description here

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