tcolorbox,lstlisting 即使对于 TeX 源也默认有悬挂缩进?

tcolorbox,lstlisting 即使对于 TeX 源也默认有悬挂缩进?

梅威瑟:

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{listings}

\tcbset{
  codefullblock/.style={
    listing only,
    listing options={
      language=TeX, style=tcblatex, texcsstyle=*\color{blue}\bfseries
    }}
}

\newtcblisting{mycodeblock}{
  codefullblock
}

\begin{document}

Try this MWE:

\begin{mycodeblock}
\documentclass{article}
\begin{document}
\TeX is a control word.

Since a control word is ended by any non-letter, the space at the end of the control word for \textbackslash TeX is not typeset as a space. Instead, it is absorbed to indicate the end of that control word.

You'll notice that the backslash control word is similarly ended by a space, and that space is not typeset as a space.

The right way to type the above sentence would be:

\TeX\ is a control word.
\end{document}
\end{mycodeblock}

\end{document}

答案1

通常,listings 会逐行逐字读取并打印输入。在您的示例中,行很长,因此它们被打断以适合页面。为了标记断行的延续,后续行会缩进 20pt(这是 的默认值listings,我没有检查tcolorbox它是否会改变)。可以使用以下命令关闭此缩进breakindent=0pt,

\documentclass{article}

\usepackage{tcolorbox}
\tcbuselibrary{listings}

\tcbset{
  codefullblock/.style={
    listing only,
    listing options={
      language=TeX, style=tcblatex, texcsstyle=*\color{blue}\bfseries,
      breakindent=0pt,
    }}
}

\newtcblisting{mycodeblock}{
  codefullblock
}

\begin{document}

\begin{mycodeblock}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc convallis, libero vel pellentesque dignissim, mi nulla lacinia neque, sit amet dapibus sem nibh non libero. Vestibulum consectetur neque eu risus aliquet, sed tincidunt felis hendrerit. Morbi et diam sed dolor posuere aliquet id quis neque. Aenean in ipsum sollicitudin, ultricies tortor non, varius nulla. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Suspendisse viverra tortor ultrices consequat tempor. Phasellus interdum mi vitae mauris dapibus, in sodales tortor tincidunt. Pellentesque sagittis eu lectus cursus rhoncus. Vivamus feugiat sit amet nisi eu rutrum. Donec mollis metus at varius ultrices. Suspendisse tempor metus a erat viverra scelerisque. Vivamus convallis, mi et pulvinar porttitor, mi orci tempor est, a eleifend velit mi at felis.

Mauris eu risus id erat iaculis sagittis. Cras auctor finibus dolor. Aliquam tincidunt ligula eu purus tristique sollicitudin. Aliquam tempor mauris viverra lorem porttitor, nec maximus nisl venenatis. Proin et mattis sapien. Vivamus auctor, nunc cursus tempus semper, felis eros sodales nisl, vitae scelerisque nibh urna quis nulla. Vestibulum congue nibh pulvinar ex suscipit fermentum. Fusce non eros lectus.
\end{mycodeblock}

\end{document}

相关内容