我无法让列表在行尾处表现并尊重页边距。请考虑以下代码:
\documentclass{article}
\usepackage{listings}
\usepackage[showframe,margin=2cm]{geometry}
\usepackage{blindtext}
\usepackage{courier}
\lstset{
language=[Visual]Basic,
basicstyle=\small\ttfamily,
breaklines=true,
numberstyle=\small\ttfamily,
}
\begin{document}
\begin{lstlisting}
Sub TEstThatLongVariableNamesDontBreakUpProperlyWithinTheLstListingEnvironmentCheckitOutForYourselfIsntThatWeird()
Debug.Print("hello world")
End Sub
\end{lstlisting}
\blindtext
Here is a very long variable name which overflows the line and needs to not do that see what I mean \lstinline!Supercalifrcexpialidocious!
\end{document}
结果如下:
尽管使用了中的选项,但 命令lstinline
和 均不lstlsting
尊重页边距。breaklines
lstset
我需要逐行逐句在页边距处正确自动地拆分。有什么办法吗?
答案1
\documentclass[UTF8]{article}
\usepackage{listings}
\usepackage{color}
\begin{document}
\lstset{
language=[Visual]Basic,
basicstyle=\small\ttfamily,
breaklines=true,
numberstyle=\small\ttfamily,
literate={T}{T}{1},%here the main feature, just a small hack you know, but it can help You, I hope
}
%\lstinputlisting{code/test.py}
\begin{lstlisting}
Sub TEstThatLongVariableNamesDontBreakUpProperlyWithinTheLstListingEnvironmentCheckitOutForYourselfIsntThatWeird()
Debug.Print("hello world")
End Sub
\end{lstlisting}
\end{document}
答案2
对于长单词,必须插入连字符号来表示断字位置。
后来,用literate
关键字将each\-
替换为红色的\hookrightarrow
。
breaklines=true
激活长行自动断线代码行。例如,如果 breakatwhitespace=true
允许仅在空白处插入换行符。
笔记
对于inline
列表,您可以添加\-
任意数量的列表(参见答案),以告诉 TeX 正确的换行符可能出现的位置。Tex 会做出(大多数情况下)不错的选择。
这样,更改文档文本就不会成为大问题。在最终版本中,与所有溢出框一样,它们需要进行目视检查。
当连字表中没有合适的位置进行连字时,对于较长的专有名词、外来词等,这是正常程序。
两种样式的区别在于红色箭头出现的位置。对于内联,最好将其放在换行符之前。
相关答案
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage[showframe,margin=5cm]{geometry}
\usepackage{blindtext}
\usepackage{courier}
% https://tex.stackexchange.com/a/168532/161015
% https://tex.stackexchange.com/a/116572/161015
\lstdefinestyle{display}% displayed listings
{
basicstyle=\small\ttfamily,
numberstyle=\small\ttfamily,
language=[Visual]Basic,
columns=fullflexible,
xleftmargin=30pt,
breaklines=true,
breakatwhitespace=true,
breakindent=30pt,
keywordstyle=\color{blue},
literate={\\-}{}{0\discretionary{}{\mbox{\textcolor{red}{$\hookrightarrow$}\space}}{}} %arrow in the new line
}
\lstdefinestyle{inline}
{%
basicstyle=\small\ttfamily,%
literate={\\-}{}{0\discretionary{\mbox{\textcolor{red}{$\hookrightarrow$}\space}}{}{}} % arrow before line break
}
\begin{document}
\begin{lstlisting}[style=display]
Sub
Public UserID As Integer TEstThatLongVariableNamesDontBreakUp\-ProperlyWithin\-TheLstListingEnvironment\-CheckitOutForYourselfIsnt\-ThatWeird()
Debug.Print("hello world")
End Sub
\end{lstlisting}
\blindtext
Here is a very long variable name which overflows the line and needs to not do that see what I mean
{\color{green!40!black}
\lstinline[style=inline]!Asanydedicatedreadercanclearly\-seeour\-apriori\-conceptsarewhatfirst\-giverisetthe\-Categories\-Hume\-tell\-sus\-that\-our\-ideasarejust\-asnecessary\-asontheother\-hand\-natural\-causes!}
\end{document}