TransWikia.com

Line break in lstlisting inside of escaped text

TeX - LaTeX Asked by schneida on February 5, 2021

I’ trying to fix the display of a line break inside an lstlisting environment, but more specifically in a part of the listing that is inside an escaped area:

documentclass{article}
usepackage[utf8]{inputenc}

usepackage[usenames,dvipsnames,table]{xcolor}
usepackage{listings}
lstset{}


definecolor{keywordColor}{HTML}{0033B3}
definecolor{parameterColor}{HTML}{067D17}
definecolor{commentColor}{HTML}{8C8C8C}
definecolor{scenarioColor}{HTML}{871094}

newcommand{lstKeyword}[1]{{textcolor{keywordColor}{#1}}}
newcommand{lstParameter}[1]{{textcolor{parameterColor}{#1}}}
newcommand{lstScenario}[1]{{textcolor{scenarioColor}{textit{#1}}}}

lstdefinestyle{bddStory}
{
  frame=single,
  captionpos=b,
  commentstyle=color{commentColor},
  keywordstyle=color{keywordColor},
  morekeywords={Given ,When ,Then ,And ,Narrative,Examples},
  escapeinside={<@}{@>},
  showspaces=false,
  showtabs=false,
  showstringspaces=false,
  breaklines=true,
  postbreak=mbox{textcolor{red}{$hookrightarrow$}space},
  breakatwhitespace=true
}

title{Example Issue}

begin{document}
begin{lstlisting}[style=bddStory]
<@lstScenario{Scenario: A scenario title is too long and can't fit into a single line which causes issues in the highlighting}@>
Given there is some further content
end{lstlisting}

end{document}

The content is renderd as follows – the two breaks in the box and the missing arrow in the new line is the issue:

wrong rendering of new line

One Answer

This segmented box is a known bug in listings package. You can use tcolorbox's listings patch to fix it. Notice that the way arguments are passed to listings package is different.

enter image description here

documentclass{article}
usepackage[utf8]{inputenc}
usepackage[usenames,dvipsnames,table]{xcolor}
usepackage{tcolorbox}

tcbuselibrary{skins, breakable, listings}

definecolor{keywordColor}{HTML}{0033B3}
definecolor{parameterColor}{HTML}{067D17}
definecolor{commentColor}{HTML}{8C8C8C}
definecolor{scenarioColor}{HTML}{871094}

newcommand{lstKeyword}[1]{{textcolor{keywordColor}{#1}}}
newcommand{lstParameter}[1]{{textcolor{parameterColor}{#1}}}
newcommand{lstScenario}[1]{{textcolor{scenarioColor}{textit{#1}}}}

lstdefinestyle{bddStory}
{
  captionpos=b,
  commentstyle=color{commentColor},
  keywordstyle=color{keywordColor},
  morekeywords={Given ,When ,Then ,And ,Narrative,Examples},
  escapeinside={<@}{@>},
  showspaces=false,
  showtabs=false,
  showstringspaces=false,
  breaklines=true,
  postbreak=mbox{textcolor{red}{$hookrightarrow$}space},
  breakatwhitespace=true
}

title{Example Issue}

newtcblisting{mylisting}[1]{
  enhanced,
  breakable,
  listing only,
  boxrule=0.8pt,
  sharp corners,
  top=0mm,
  bottom=0mm,
  left=2mm,
  right=2mm,
  boxsep=0mm,
  colframe=black,
  colback=white,
  listing options={
    style=#1
  }
}


begin{document}
begin{mylisting}{bddStory}
<@lstScenario{Scenario: A scenario title is too long and can't fit into a single line which causes issues in the highlighting}@>
Given there is some further content
end{mylisting}


end{document}

As for the missing arrow, I think listings package does not try to add postbreak contents to escaped region. See the example below:

enter image description here

If you really want to implement this feature, I find these two posts:

Answered by Alan Xiang on February 5, 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