在列表中,断线(启用断线选项)会导致框架绘制不正确。如何修复此问题?

在列表中,断线(启用断线选项)会导致框架绘制不正确。如何修复此问题?

我有一个lstlistingbreaklines启用frame的。实际设置如下。

\lstset{
    basicstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    keywordstyle={},
    frame=single,
    frameround=tttt,
    rulecolor=\color{gray},
    escapechar=\&
}

问题是,每当一行太长而分成两行时,它周围的框架就不会包含第二行。下图显示了它的样子:

列表周围的框架损坏

编辑:好吧,我发现我遗漏了一些重要信息。我自己设计了列表样式(而不是自动设计),如您在图片中看到的那样。这就是我使用&as 的原因escapechar,并且实际上对列表中的所有内容进行了转义。请参阅此代码(请注意,这是软件生成的!):

\documentclass{article}
\usepackage{listings, xcolor}

\lstset{
        basicstyle=\ttfamily,
        showstringspaces=false,
        breaklines=true,
        keywordstyle={},
        frame=single,
        frameround=tttt,
        rulecolor=\color{gray},
        escapechar=\&
}
\begin{document}
\begin{lstlisting}
&\#include <stdio.h>&
&int main()&
&\{&
        &\textcolor[rgb]{0.0,0.2,0.4}{printf("Enter a, b, and c (space separated) in the following equation:\textbackslash{}nax\^{}2+bx+c = 0\textbackslash{}n");}&
        &\textcolor[rgb]{0.0,0.2,0.4}{scanf("\%f \%f \%f", \&a, \&b, \&c);}&
        &\textcolor[rgb]{0.0,0.2,0.4}{printf("What happens when I type two less{-}than signs like this? << \textbackslash{}n");}&
&\}&
\end{lstlisting}
\end{document}

在此代码中,我只是用来\textcolor演示,尽管这主要是我所做的。

此代码产生以下输出:

在此处输入图片描述

这是pdflatex的输出:

This is pdfTeX, Version 3.1415926-1.40.11 (TeX Live 2010)
 restricted \write18 enabled.
entering extended mode
(./a.tex
LaTeX2e <2009/09/24>
Babel <v3.8l> and hyphenation patterns for english, dumylang, nohyphenation, ge
rman-x-2009-06-19, ngerman-x-2009-06-19, afrikaans, ancientgreek, ibycus, arabi
c, armenian, basque, bulgarian, catalan, pinyin, coptic, croatian, czech, danis
h, dutch, ukenglish, usenglishmax, esperanto, estonian, ethiopic, farsi, finnis
h, french, galician, german, ngerman, swissgerman, monogreek, greek, hungarian,
 icelandic, assamese, bengali, gujarati, hindi, kannada, malayalam, marathi, or
iya, panjabi, tamil, telugu, indonesian, interlingua, irish, italian, kurmanji,
 lao, latin, latvian, lithuanian, mongolian, mongolianlmc, bokmal, nynorsk, pol
ish, portuguese, romanian, russian, sanskrit, serbian, slovak, slovenian, spani
sh, swedish, turkish, turkmen, ukrainian, uppersorbian, welsh, loaded.
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/article.cls
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
(/usr/local/texlive/2010/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2010/texmf-dist/tex/latex/listings/listings.sty
(/usr/local/texlive/2010/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2010/texmf-dist/tex/latex/listings/lstmisc.sty)
(/usr/local/texlive/2010/texmf-dist/tex/latex/listings/listings.cfg))
(/home/sciabaz/texmf/tex/latex/xcolor.sty
(/usr/local/texlive/2010/texmf-dist/tex/latex/latexconfig/color.cfg)
(/usr/local/texlive/2010/texmf-dist/tex/latex/pdftex-def/pdftex.def)) (./a.aux)
(/usr/local/texlive/2010/texmf-dist/tex/context/base/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
)

LaTeX Font Warning: Font shape `OMS/cmtt/m/n' undefined
(Font)              using `OMS/cmsy/m/n' instead
(Font)              for symbol `textbraceleft' on input line 19.

[1{/usr/local/texlive/2010/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./a.aux)

LaTeX Font Warning: Some font shapes were not available, defaults substituted.

 )</usr/local/texlive/2010/texmf-dist/fonts/type1/public/amsfonts/cm/cmr10.pfb>
</usr/local/texlive/2010/texmf-dist/fonts/type1/public/amsfonts/cm/cmsy10.pfb><
/usr/local/texlive/2010/texmf-dist/fonts/type1/public/amsfonts/cm/cmtt10.pfb></
usr/local/texlive/2010/texmf-dist/fonts/type1/public/amsfonts/latxfont/lcircle1
.pfb>
Output written on a.pdf (1 page, 34186 bytes).
Transcript written on a.log.

请注意,使用 Mike 的代码,我没有收到字体警告。

答案1

我认为发生的情况是,由于在转为 LaTeX 时会产生多条线,因此环境lstlisting没有机会在左侧和右侧添加垂直规则。

解决此问题的一种简单方法是使用其他工具将框架实际放在代码周围。以下是使用环境的示例mdframed(但任何其他替代方案也同样有效)来生成:

在此处输入图片描述

\documentclass{article}
\usepackage{listings, xcolor}
\usepackage[framemethod=TikZ]{mdframed}

\lstset{
        basicstyle=\ttfamily,
        showstringspaces=false,
        breaklines=true,
        keywordstyle={},
        frame=none,%              changed to "none"
        %frameround=tttt,%        commented as not relevant with frame=none
        rulecolor=\color{gray},
        escapechar=\&
}
\begin{document}
\begin{mdframed}[roundcorner=5pt]
\begin{lstlisting}
&\#include <stdio.h>&
&int main()&
&\{&
        &\textcolor[rgb]{0.0,0.2,0.4}{printf("Enter a, b, and c (space separated) in the following equation:\textbackslash{}nax\^{}2+bx+c = 0\textbackslash{}n");}&
        &\textcolor[rgb]{0.0,0.2,0.4}{scanf("\%f \%f \%f", \&a, \&b, \&c);}&
        &\textcolor[rgb]{0.0,0.2,0.4}{printf("What happens when I type two less{-}than signs like this? << \textbackslash{}n");}&
&\}&
\end{lstlisting}
\end{mdframed}
\end{document}

答案2

我无法使用以下 MWE 重现您的任何一个问题:

\documentclass{article}
\usepackage{listings,xcolor}
\lstset{
    basicstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    keywordstyle=\color{red},
    frame=single,
    frameround=tttt,
    rulecolor=\color{gray},
}
\begin{document}
\begin{lstlisting}[language=C]
#include <stdio.h>
int main()
{
  printf("Enter a, b, and c (space separated) in the following equation:\nax^2+bx+c = 0\n");
  scanf("%f %f %f", &a, &b, &c);
  printf("What happens when I type two less-than signs like this? <<\n");
}
\end{lstlisting}
\end{document}

在此处输入图片描述

使用我的代码,您是否会得到相同的结果?如果没有,您使用的是哪种 TeX 发行版?如果是,您能否提供一个显示问题的替代 MWE?

编辑:

我怀疑您使用的方法会导致大量额外的工作,而且由于我不确定您真正想要完成什么(为什么您几乎在所有地方都逃避?),这里有第二个示例 - 第一部分以相同的方式为所有标识符着色,第二部分为特定的标识符着色,而其他标识符则保持未格式化。

\documentclass{article}
\usepackage{listings,xcolor}
\lstset{
    basicstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    %keywordstyle=\color{red},
    %identifierstyle=\color{blue},
    %stringstyle=\color{green},
    frame=single,
    frameround=tttt,
    rulecolor=\color{gray},
}
\begin{document}
\begin{lstlisting}[language=C,identifierstyle=\color{blue},caption={highlight all identifiers}]
#include <stdio.h>
int main()
{
  float a, b, c;
  printf("Enter a, b, and c (space separated) in the following equation:\nax^2+bx+c = 0\n");
  scanf("%f %f %f", &a, &b, &c);
  printf("What happens when I type two less-than signs like this? <<\n");
}
\end{lstlisting}
\begin{lstlisting}[language=C,
  emph={printf},emphstyle=\color{blue},
  emph={[2]{scanf}},emphstyle={[2]\color{magenta}},
  caption={highlight specific words}
]
#include <stdio.h>
int main()
{
  float a, b, c;
  printf("Enter a, b, and c (space separated) in the following equation:\nax^2+bx+c = 0\n");
  scanf("%f %f %f", &a, &b, &c);
  printf("What happens when I type two less-than signs like this? <<\n");
}
\end{lstlisting}
\end{document}

在此处输入图片描述

相关内容