下图显示了我的问题:
我如何知道\lstinline
在特定的 C++ 操作符处换行?例如:FooFactory::instance()->createFoo()
应按如下方式换行:
... text FooFactory::
instance()->createFoo()
或者
FooFactory::instance()->
createFoo() text text ...
梅威瑟:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[left=6cm,right=7cm]{geometry}
\usepackage{parskip}
\usepackage{showframe}
\usepackage{listings}
\lstset{
language=C++,
basicstyle=\ttfamily,
breaklines=true,
breakatwhitespace=true,
inputencoding=utf8,
extendedchars=true
}
\lstset{literate=% I dont know what I am doing...
{::}{::}{1\discretionary{}{}{}} % line-break at ::
{->}{->}{1\discretionary{}{}{}} % line-break at ->
}
\begin{document}
text text text text text text text text \lstinline{FooFactory::instance()->createFoo()} text text
text text text text
text text text text text text \lstinline{FooFactory::instance()->createFoo()} text text
text text text text
\end{document}
答案1
我也不明白为什么这样做有效。更准确地说,我不知道为什么需要将 \discretionary 放在数字后面,而不是放在替换文本中。
\documentclass{article}
%\usepackage[utf8]{inputenc}% all it does for me is produce warnings
\usepackage[left=6cm,right=7cm]{geometry}
\usepackage{parskip}
\usepackage{showframe}
\usepackage{listings}
\lstset{
language=C++,
basicstyle=\ttfamily,
breaklines=true,
breakatwhitespace=true,
inputencoding=utf8,
extendedchars=true
}
\lstset{literate={::}{}{0\discretionary{::}{}{::}}% line-break at ::
{->}{}{0\discretionary{->}{}{->}}% line-break at ->
}
\begin{document}
text text text text text text text text \lstinline{FooFactory::instance()->createFoo()} text text text text text text
text text text text text text \lstinline{FooFactory::instance()->createFoo()} text text text text text text
\begin{lstlisting}
FooFactory::instance()->createFoo();
\end{lstlisting}
\end{document}