2

I would like to use listings to format bash commands in such a way that overly long lines are automatically broken with a backslash. I know about the prebreak option, but it appears as though it is impossible to pass a literal backslash character to prebreak.

I learned from this answer that I could specify backslashes with \textbackslash, but using it produces the following warning message:

LaTeX Font Warning: Font shape `OMS/cmtt/m/n' undefined
(Font) using `OMS/cmsy/m/n' instead for symbol `textbackslash' on input line 8.

While this still allows for document compilation, it looks like the \textbackslash somehow avoids being formatted according to the basicstyle option. As seen in the below example, listings is perfectly capable of correctly styling backslashes that are manually entered.

Is it possible to make listings automatically break long lines with a backslash that is correctly styled?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}

\begin{document} % Automatic line break \begin{lstlisting}[basicstyle=\ttfamily,breaklines,breakatwhitespace,prebreak={\textbackslash}] long string of text to test line breaking for the listings package \end{lstlisting}

% Manual line break \begin{lstlisting}[basicstyle=\ttfamily,breaklines,breakatwhitespace] long string of text to test line breaking for the
listings package \end{lstlisting} \end{document}

Screenshot of output of above latex code, demonstrating the visual difference in backslashes

JAK Zero
  • 212
  • 1
    Try adding \usepackage[T1]{fontenc}. – campa Nov 10 '20 at 12:41
  • @campa That seems to have worked perfectly. I would mark your comment as the answer, but that doesn't seem to be possible. – JAK Zero Nov 10 '20 at 12:46
  • Well, it's a comment, not an answer :-). But this is a question which pops up every once in a while so there is surely a duplicate somewhere. Alas, I have no time to search for it right now. – campa Nov 10 '20 at 12:47
  • @campa OK, what should I do now? I don't know how to "close" a question without accepting an answer. – JAK Zero Nov 10 '20 at 12:50
  • @JAKZero Make your own answer what campa suggested... – MadyYuvi Nov 10 '20 at 13:08
  • @MadyYuvi According to this page, I cannot accept my own answer within 48 hours of creating the question. I guess I'll wait until 48 hours have passed and then I'll try that? – JAK Zero Nov 10 '20 at 13:14

1 Answers1

0

I have found two answers to my question, both of which allow backslashes to be correctly styled when used for automatic line-breaking with listings:

  • campa's suggestion of add \usepackage[T1]{fontenc} to the preamble fixes the styling issue when \textbackslash is used for the prebreak option.

  • An alternative solution was found in the question that I had linked to in my own question. While this question concerned the return symbol, the example code they provided includes a different method of specifying backslashes with \char`\\. It turns out that setting prebreak with \char`\\ results in a correctly styled backslash, without having to use \usepackage[T1]{fontenc}.

It was very helpful to learn about \char, as I expect that it may be necessary for passing special characters to options in future circumstances. However, for this particular situation and for writing with LaTeX in general, I think I will stick with using \usepackage[T1]{fontenc} due to the benefits outlined in this answer.

JAK Zero
  • 212