3

The example illustrates my problem. I have long code rows which should break automatically (see line 1) but some lines I want to break manually (line 2 / 3). How can I do this?

I have played a little bit around and example 1 could be a solution but it doesn't work in figure environments (example2).

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{listings}

\lstset{
   breakatwhitespace=false,
   breaklines=true,
   escapeinside={(*@}{@*)},
   frame=lines,
   numbers=left,
   postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\rightarrow\space}},
   showlines=true
}

\begin{document}

Example 1:
\begin{lstlisting}[numbers=left]
a = b + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c
a = b (*@\\\ensuremath{\rightarrow\space}@*)  + c
\end{lstlisting}
\vspace{2cm}
Example 2:
\begin{figure}[h]
\centering
\begin{lstlisting}[numbers=left]
a = b + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c
a = b (*@\\\ensuremath{\rightarrow\space}@*)  + c
\end{lstlisting}
\end{figure}

\end{document}

enter image description here

4

The culprit is the \centering you are issuing inside the figure, not the figure itself.

Removing it the problem is solved.

MWE

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{listings}

\lstset{
   breakatwhitespace=false,
   breaklines=true,
   escapeinside={(*@}{@*)},
   frame=lines,
   numbers=left,
   postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\rightarrow\space}},
   showlines=true
}

\begin{document}

Example 1:
\begin{lstlisting}[numbers=left]
a = b + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c
a = b (*@\\\ensuremath{\rightarrow\space}@*)  + c
\end{lstlisting}
\vspace{2cm}
Example 2:
\begin{figure}[h]
\begin{lstlisting}[numbers=left]
a = b + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c + c
a = b (*@\\\ensuremath{\rightarrow\space}@*)  + c
\end{lstlisting}
\end{figure}

\end{document} 

Output

enter image description here

  • Arghhh... Thanks, Latex is sometimes so illogical... – user4811 Jan 14 '14 at 21:58

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.