我正在使用lstinputlisting
从外部文件读取文本。
\lstinputlisting[breaklines]{text.txt}
该文件包含一行很长的内容:
ssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringgggggggggggggggggggggggg ssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringgggggggggggggggggggggggg ssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringgggggggggggggggggggggggg ssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringgggggggggggggggggggggggg ssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringgggggggggggggggggggggggg ssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringgggggggggggggggggggggggg ssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringgggggggggggggggggggggggg ssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringggggggggggggggggggggggggssssssssssssssssssssssssssstrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrringgggggggggggggggggggggggg
我想将该行分成几行连续的行,但它显示“尺寸太大”的错误。
答案1
根据对我的问题的评论,我找到了解决方案:
\documentclass{article}
\usepackage{listings}
\makeatletter
\def\@tempA#1#2\@end
{
\@tempA@{#1}
\ifx\relax#2\relax
\else\@tempA#2\@end
\fi
}
\def\@tempA@#1
{
{\noexpand#1}
{{\char`\noexpand#1\allowbreak}}1
}
\edef\@tempB
{
\noexpand\lstdefinelanguage{text}
{
keepspaces,
breaklines,
literate=\@tempA !"\#\$\%\&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz\{|\}~\@end
}
}
\@tempB
\makeatother
\newlength\basewidth
\settowidth\basewidth{\ttfamily\tiny X}
\lstset
{
language=text,
basicstyle=\ttfamily\tiny,
basewidth=\basewidth,
breakindent=4\basewidth,
postbreak=\llap{$\hookrightarrow$}
}
\begin{document}
\lstinputlisting{text.txt}
\end{document}
该解决方案主要基于这个答案。我不知道上面的代码具体是如何工作的,但它确实有效。
奇怪的是,如果行太大,即行跨越两页或更多页,这种方法仍然不起作用。在这种情况下,仍然会显示错误“尺寸太大”。一种可能的解决方法是减小字体大小和页边距,以便行可以容纳在两页以内,尽管这不是一个完美的解决方案。