2

When using the lstlisting environment, I can't get it to break on a long line which contains only the * character. This is my minimum working example. It produces a listing in which the text flows out of the listing's frame and does not produce correct line breaks. How do I fix this? I have tried different parameters in \lstset, but nothing seems to work. How do I tell the listing environment that it's okay to break on a *?

enter image description here

\usepackage{listings}
\begin{document}
\lstset{%
    breaklines=true,    
    breakatwhitespace=false,
    postbreak=\space,   
    tabsize=2,      
    basicstyle=\ttfamily\footnotesize, 
    showspaces=false,   
    showstringspaces=false,
    extendedchars=true, 
    frame=single,       
    frameround=ffff,
    aboveskip=1.5em
}
\begin{lstlisting}
    **************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** hello world
\end{lstlisting}
\end{document}
1

The way text is broken in listings environment depends on the selected source code language. If you don't want to change the language setting, you can add the option literate={*}{*\allowbreak}1 which replaces each * character by a version that allows a linebreak right after it.

Updated example:

\documentclass{article}
\usepackage{listings}
\begin{document}
\lstset{%
    breaklines=true,    
    breakatwhitespace=false,
    postbreak=\space,   
    tabsize=2,      
    basicstyle=\ttfamily\footnotesize, 
    showspaces=false,   
    showstringspaces=false,
    extendedchars=true, 
    frame=single,       
    frameround=ffff,
    aboveskip=1.5em,
    literate={*}{*\allowbreak}1  % ADDED
}
\begin{lstlisting}
    **************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************** hello world
\end{lstlisting}
\end{document}

enter image description here

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.