我无法使用前面问题中描述的任何方法让列表包换行: Listings 包未破,https://stackoverflow.com/questions/981020/how-to-force-line-wrapping-in-listings-package
mwrep
仅当使用文档类时才会出现此问题。Inreport
或article
line-breaks 都可以正常工作。
这是我的 MWE:
\documentclass{mwrep}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[ breaklines=true ]
Some text with a long line that needs to be broken but is not with mwrep documentclass. What should I do about it?
\end{lstlisting}
\end{document}
答案1
我发现罪魁祸首是mwrep
文档类以及它在其源中设置的事实\exhyphenpenalty=10000
,这会干扰列表包换行的能力。
我发现的解决方案是在列表之前将其改回 100,并在列表结束时恢复更改的值:
\documentclass{mwrep}
\usepackage{listings}
\usepackage{etoolbox}
\BeforeBeginEnvironment{lstlisting}{\exhyphenpenalty=100}
\AfterEndEnvironment{lstlisting}{\exhyphenpenalty=10000}
\begin{document}
\begin{lstlisting}[ breaklines=true ]
Some text with a long line that needs to be broken but is not with mwrep documentclass. What should I do about it?
\end{lstlisting}
\end{document}