在列表换行符上显示回车符号

在列表换行符上显示回车符号

我想知道是否有可能列表包裹显示退货符号,如““当出现换行符时。

使用默认行号设置时不需要这样做,因为换行符会在两个连续的行号之间创建一个空白,但如果使用间隔行号(例如 1-5-10-15-20)且仅编号几行,则这可能会很有用。

我的想法是重新定义换行符宏,搜索“ifbreaklines”(选项名称)lstmisc.sty,但我找不到任何实施指南,并且文件也没有注释。

编辑:添加的标记是行的一部分prebreak={}postbreak={}随之复制,因此它们不是理想的解决方案(并且您不能在那里使用unicode字符或宏)

如果有人愿意提供帮助和实验,MWE:

\documentclass[a4paper,12pt]{article}
\usepackage{xcolor}
\usepackage{listings}

\lstset{
    basicstyle=\footnotesize\ttfamily,
    breakatwhitespace=true,  
    breaklines=true,         
    firstnumber=1,
    frame=leftline,       
    keepspaces=true,         
    numbers=left,            
    showspaces=false,        
    showstringspaces=false,  
    showtabs=false,          
    stepnumber=1,
    language=java,
    numberstyle=\color{darkgray},
    commentstyle=\color{gray},     
    stringstyle=\color{red},
    keywordstyle=\color{blue},
    rulecolor=\color{black}
}

\begin{document}

\begin{lstlisting}[caption={You can easily recognize line breaks}]
public int nextInt(int n) {
 if (n<=0)
  throw new IllegalArgumentException("n is not positive");


 // this is a very very very very very very very very very very very very very very very very very very very very very very very very very very very long line
 if ((n & -n) == n)  // i.e., n is a power of 2
  return (int)((n * (long)next(31)) >> 31);

 int bits, val;
 do {
  bits = next(31);
  val = bits % n;
 } while(bits - val + (n-1) < 0);
 return val;
}
\end{lstlisting}


\begin{lstlisting}[stepnumber=5,caption={You cannot easily tell where line breaks occur}]
public int nextInt(int n) {
 if (n<=0)
  throw new IllegalArgumentException("n is not positive");

  
 // this is a very very very very very very very very very very very very very very very very very very very very very very very very very very very long line
 if ((n & -n) == n)  // i.e., n is a power of 2
  return (int)((n * (long)next(31)) >> 31);

 int bits, val;
 do {
  bits = next(31);
  val = bits % n;
 } while(bits - val + (n-1) < 0);
 return val;
}
\end{lstlisting}

\end{document}

在此处输入图片描述

答案1

在回答另一个答案时,我发现tikzmarkTi 的库Z 提供了一些与软件包一起使用的功能listings。以下内容仍需要您手动放置返回标记,因此它不是完全自动完成的。但它可能仍然比没有返回标记要好...

该宏有两个参数,第一个参数表示相关行号,第二个参数表示换行符数。请注意,我假设列表环境的行距为 1em,但在其他情况下可能会有所不同。

\documentclass[a4paper,12pt]{article}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}

\newcommand{\continuingline}[2]{%
    \begin{tikzpicture}[remember picture, overlay]
        \foreach \i in {1,...,#2} {
            \node[anchor=base, align=center] at ([xshift=-1.25em, yshift={-\i*1em}]pic cs:line-code-#1-start) {$\hookrightarrow$};
        }
    \end{tikzpicture}%
}

\lstset{
    basicstyle=\footnotesize\ttfamily,
    breakatwhitespace=true,  
    breaklines=true,         
    firstnumber=1,
    frame=leftline,       
    keepspaces=true,         
    numbers=left,            
    showspaces=false,        
    showstringspaces=false,  
    showtabs=false,          
    stepnumber=1,
    language=java,
    numberstyle=\color{darkgray},
    commentstyle=\color{gray},     
    stringstyle=\color{red},
    keywordstyle=\color{blue},
    rulecolor=\color{black},
}

\begin{document}

\begin{lstlisting}[caption={You can easily recognize line breaks}, name=code]
public int nextInt(int n) {
 if (n<=0)
  throw new IllegalArgumentException("n is not positive");


 // this is a very very very very very very very very very very very very very very very very very very very very very very very very very very very long line
 if ((n & -n) == n)  // i.e., n is a power of 2
  return (int)((n * (long)next(31)) >> 31);

 int bits, val;
 do {
  bits = next(31);
  val = bits % n;
 } while(bits - val + (n-1) < 0);
 // this is another although not very very very very long but still quite long line
 return val;
}
\end{lstlisting}

\continuingline{6}{2}
\continuingline{15}{1}

\end{document}

在此处输入图片描述


更新:更好的解决方案,不仅适用于多个lstlisting环境。

该宏现在有三个强制参数,第一个是列表的名称,必须通过name=...相关lstlisting环境的选项进行设置。第二个和第三个参数分别是相关行号和换行符数。

此外,如果行间距不是 1em,则该宏会采用一个可选参数。默认值为 1em。

\continuingline[<lineskip> ]{<列表名称> }{<行号> }{<换行符>}

\documentclass[a4paper,12pt]{article}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{tikz}
\usetikzlibrary{tikzmark}
\usetikzmarklibrary{listings}

\newcommand{\continuingline}[4][1em]{%
    \begin{tikzpicture}[remember picture, overlay]
        \foreach \i in {1,...,#4} {
            \node[anchor=base, align=center] at ([xshift=-1.25em, yshift={-\i*#1}]pic cs:line-#2-#3-start) {$\hookrightarrow$};
        }
    \end{tikzpicture}%
}

\lstset{
    basicstyle=\footnotesize\ttfamily,
    breakatwhitespace=true,  
    breaklines=true,         
    firstnumber=1,
    frame=leftline,       
    keepspaces=true,         
    numbers=left,            
    showspaces=false,        
    showstringspaces=false,  
    showtabs=false,          
    stepnumber=1,
    language=java,
    numberstyle=\color{darkgray},
    commentstyle=\color{gray},     
    stringstyle=\color{red},
    keywordstyle=\color{blue},
    rulecolor=\color{black},
}

\begin{document}

\begin{lstlisting}[caption={You can easily recognize line breaks}, name=codea]
public int nextInt(int n) {
 if (n<=0)
  throw new IllegalArgumentException("n is not positive");


 // this is a very very very very very very very very very very very very very very very very very very very very very very very very very very very long line
 if ((n & -n) == n)  // i.e., n is a power of 2
  return (int)((n * (long)next(31)) >> 31);

 int bits, val;
 do {
  bits = next(31);
  val = bits % n;
 } while(bits - val + (n-1) < 0);
 // this is another although not very very very very long but still quite long line
 return val;
}
\end{lstlisting}

\continuingline{codea}{6}{2}
\continuingline{codea}{15}{1}

\begin{lstlisting}[caption={You can again easily recognize line breaks}, name=codeb]
public int nextInt(int n) {
 if (n<=0)
  throw new IllegalArgumentException("n is not positive");
  // this is another unbelievably long and probably under normal circumstances totally unnesessary line
 return val;
}
\end{lstlisting}

\continuingline{codeb}{4}{1}

\begin{lstlisting}[caption={You can again easily recognize line breaks}, name=codec, basicstyle=\footnotesize\ttfamily\linespread{1.2}\selectfont]
public int nextInt(int n) {
 if (n<=0)
  throw new IllegalArgumentException("n is not positive");
  // this is yet another unbelievably long and probably under normal circumstances totally unnesessary line
 return val;
}
\end{lstlisting}

\continuingline[1.2em]{codec}{4}{1}

\end{document}

在此处输入图片描述

答案2

我找到了合适的宏!

Jasper 建议使用tikzmarklistings 库,这让我能够更好地理解底层listings代码

在以下位置搜索“postbreak” lstmisc.sty

\gdef\lst@@discretionary{%
    \discretionary{\let\space\lst@spacekern\lst@prebreak}%
                  {\llap{\lsthk@EveryLine
                   \kern\lst@breakcurrindent \kern-\@totalleftmargin}%
                   \let\space\lst@spacekern\lst@postbreak}{}}

现在将其复制到当前文档并找到放置符号的正确位置:

\makeatletter
\gdef\lst@@discretionary{%
    \discretionary{\let\space\lst@spacekern\lst@prebreak}%
                  {\llap{HERE \lsthk@EveryLine
                   \kern\lst@breakcurrindent \kern-\@totalleftmargin}%
                   \let\space\lst@spacekern\lst@postbreak}{}}
\makeatother

有人可能想使用相同的行号颜色,在这种情况下,将“HERE”替换为 就足够了{\color{darkgray}→\ }。我使用“→”,因为我想要的 unicode 符号没有出现。

结果:

在此处输入图片描述

相关内容