0

I would like to justify text in lstlisting. If the line gets too long, there doesn't seem to be any way to automatically add a new line.

Is there a way to justify text?

8
  • 2
    use breaklines=true Jun 4, 2023 at 14:28
  • Would it hyphenate long words?
    – Dunno
    Jun 4, 2023 at 14:45
  • 2
    no, but linebreaking listings is already unusual (lines are significant in most programming languages) hyphenating would be very weird, what is your use case here?? listings is for syntax highlighting verbatim code listings, if you add hyphens you would get invalid code display. Jun 4, 2023 at 14:53
  • I would only use it for comments in pseudocode. I would like to add justified block of comments. Anyway all the languages I used allow breaking conditional statements.
    – Dunno
    Jun 4, 2023 at 15:05
  • 1
    i don't think justification is possible, it seems at odds with the basic design of listings which isnt using paragraphs internally Jun 4, 2023 at 15:10

1 Answer 1

0

This is the best solution I have figured out. Let see if it fits you:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
    basicstyle=\ttfamily,
    columns=fullflexible,
    frame=single,
    breaklines=true,
    postbreak=\mbox{\textcolor{red}{$\hookrightarrow$}\space},
}
\begin{document}
\begin{lstlisting}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{lstlisting}
\end{document}

enter image description here

Or without the arrows:

\documentclass{article}
\usepackage{listings}
  
\lstset{
    basicstyle=\ttfamily,
    columns=fullflexible,
    frame=single,
    breaklines=true,
}
\begin{document}
\begin{lstlisting}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
\end{lstlisting}
\end{document}

enter image description here

You must log in to answer this question.

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