TransWikia.com

Color problem in listings with breaklines=true

TeX - LaTeX Asked by 3isenHeim on January 19, 2021

I’m trying to highlight a specific comment type with lstsliting, and the option breaklines messes things over.

Here is the faulty output :
Faulty output

The color purple (produced by the keyword name) takes over, and colors the rest of the listing.

Here is what I need (and hat should normally produce) in terms of colors :
Desired output

Here is the MWE :

documentclass{article}
usepackage[]{geometry}
usepackage[dvipsnames,svgnames,usenames,table]{xcolor} % Smart color names
usepackage{listings}


lstdefinestyle{system}{%
%    breaklines=true,   % <-- This makes the color scheme go nuts
    breakatwhitespace=true, % <-- Even with this
    keywordstyle=color{Blue},
    basicstyle=normalfontttfamily,
    morecomment=[s][color{Blue}]{[*}{]},
    morecomment=[s][color{Green}]{[+}{]},
    morecomment=[s][color{Red}]{[-}{]},
    morecomment=[n][color{Purple}]{(name:}{)},
    morecomment=[n][color{Gray}]{(domain:}{)},
}


begin{document}


begin{lstlisting}[style=system]                  
test      X:445 Test     [+] Success1        (name:system)   (domain:web)
test      X:445 Test     [-] Failure 10.0    (name:system)   (domain:web)
test      X:445 Test     [*] Don't know 10.0 (name:system)   (domain:web)
end{lstlisting}

end{document}

Does anybody know why the breaklines options messes the colored filter ?
In the content of the listing, I can’t get rid of all the spaces, so removing them to avoid a line break is not an option.

I want to be able to have the colored filters AND the line break.

2 Answers

Inspired from this answer, the solution is to patch the command lsthk@SelectCharTable.

The corrected MWE is thus :

documentclass{article}
usepackage[]{geometry}
usepackage[dvipsnames,svgnames,usenames,table]{xcolor} % Smart color names
usepackage{listings}
usepackage{etoolbox}

makeatletter
patchcmd{lsthk@SelectCharTable}{)}{`}{}{} 
makeatother 


lstdefinestyle{system}{%
    breaklines=true,
    breakatwhitespace=true,
    keywordstyle=color{Blue},
    basicstyle=normalfontttfamily,
    morecomment=[s][color{Blue}]{[*}{]},
    morecomment=[s][color{Green}]{[+}{]},
    morecomment=[s][color{Red}]{[-}{]},
    morecomment=[n][color{Purple}]{(name:}{)},
    morecomment=[n][color{Gray}]{(domain:}{)},
}


begin{document}


begin{lstlisting}[style=system]                  
test      X:445 Test     [+] Success1        (name:system)   (domain:web)
test      X:445 Test     [-] Failure 10.0    (name:system)   (domain:web)
test      X:445 Test     [*] Don't know 10.0 (name:system)   (domain:web)
end{lstlisting}

end{document}

And the output is properly colored :

MWE output

Correct answer by 3isenHeim on January 19, 2021

I don't know if this is the best solution, but it does work if you remove the ambiguity in the closing delimiter:

documentclass{article}
usepackage{geometry}
usepackage[dvipsnames,svgnames,usenames,table]{xcolor}
usepackage{listings}

lstdefinestyle{system}{%
    breaklines=true,
    keywordstyle=color{Blue},
    basicstyle=normalfontttfamily,
    morecomment=[s][color{Blue}]{[*}{]},
    morecomment=[s][color{Green}]{[+}{]},
    morecomment=[s][color{Red}]{[-}{]},
    morecomment=[n][color{Purple}]{(name:}{m)},
    morecomment=[n][color{Gray}]{(domain:}{b)},
}

begin{document}

begin{lstlisting}[style=system]                  
test      X:445 Test     [+] Success1        (name:system)   (domain:web)
test      X:445 Test     [-] Failure 10.0    (name:system)   (domain:web)
test      X:445 Test     [*] Don't know 10.0 (name:system)   (domain:web)
end{lstlisting}

end{document}

output

Answered by David Purton on January 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP