4

I would like to show a couple of lines from a CSV file using the listings package with breaklines=true.

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{%
  backgroundcolor=\color{lightgray},
  basicstyle=\ttfamily\footnotesize,
  breaklines,
  showspaces
}

\begin{document}
\begin{lstlisting}
K01980;23S ribosomal RNA;11.79648646;17.54756407;16.55812848;7.070375553;9.904088434;10.33047681;11.34235283;12.84408234
K01977;16S ribosomal RNA;7.433653271;11.21805284;10.47502621;4.555449801;6.223779066;6.547853101;7.12047883;8.056722
K03046;DNA-directed RNA polymerase subunit beta' [EC:2.7.7.6];1.279373326;1.843958244;1.291530419;0.91598316;1.444456949;1.370082994;1.471863596;1.274232464
\end{lstlisting}
\end{document}

The output is not quite satisfactory, though.

enter image description here

  • The break occurs very early, so there is a lot of empty space at the end of the first line and the second line exceeds the available text width. There is no line break in the second line.
  • There is a blank line after each line.

I would like to have something like this:

K01980;23S ribosomal RNA;11.79648646;17.54756407;16.55812848;
  7.070375553;9.904088434;10.33047681;11.34235283;12.84408234
K01977;16S ribosomal RNA;7.433653271;11.21805284;10.47502621;
  4.555449801;6.223779066;6.547853101;7.12047883;8.056722
K03046;DNA-directed RNA polymerase subunit beta' [EC:2.7.7.6];
  1.279373326;1.843958244;1.291530419;0.91598316;1.444456949;
  1.370082994;1.471863596;1.274232464
  • I would use rather a tabular environment for this than lstlisting – user31729 Feb 14 '18 at 12:59
  • I do that too, but the point here is to show how raw data is represented. – tucuxi Feb 14 '18 at 13:20
  • There is no indication about raw data in your question, however – user31729 Feb 14 '18 at 13:23
  • I would like to show a couple of lines from a CSV file? – tucuxi Feb 14 '18 at 13:26
  • 1
    I think the question is still relevant, as it is obviously not evident how to make listlistings break at any character and not only at spaces. Or is it? – Jasper Habicht Feb 14 '18 at 13:32
2

listings doesn't break chunks of charcters when they belong to the same internal category. In this case, the letter, digit and other categories are of special interest. By default, characters are assigned to categories like you would expect, i.e. letters are of category letter, digits are digit, and symbols are other.

When a letter is found, all following letter or digit characters are read until a non-letter/non-digit is found. This series is then output as one chunk. The same now happens with all characters that are non-letter until the next letter is found. Processing the input in that way prevents the long series of digits, periods and semicolons in your example from being broken, because no letter is found to start a new chunk.

Here are to proposals how to solve this problem:

  • Rearrange the character categories such that the built chunks correspond to the logical data units of your specific usecase. For example you might want to move digits and the period also to category letter, while leaving the semicolon as category other. This can be done by adding

    alsoletter={0123456789.}
    

    to \lstset. Line breaks now can occur also after all decimal numbers and after the semicolon.

  • More straight forward is to use the literate option and redefine the semicolon to a version that allows a linebreak after the symbol:

    literate={;}{{;\allowbreak}}1
    

Both solutions now give the more appealing result

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.