轉義文字內 lstlisting 中的換行符

轉義文字內 lstlisting 中的換行符

我試圖修復 lstlisting 環境中換行符的顯示,但更具體地說是在轉義區域內的列表部分:

\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}

內容呈現如下 - 框中的兩個中斷和新行中缺少的箭頭是問題:

新行渲染錯誤

答案1

這個分段的盒子是包中的一個已知錯誤listings。您可以使用tcolorboxlistings補丁來修復它。請注意,參數傳遞給包的方式listings是不同的。

在此輸入影像描述

\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}

至於缺少的箭頭,我認為listings包不會嘗試將postbreak內容添加到轉義區域。請參閱下面的範例:

在此輸入影像描述

如果你真的想要實現這個功能,我找到了這兩篇文章:

相關內容