允许在 \lstinline 中的 C++ 运算符 ('::'、'->'、...) 处换行

允许在 \lstinline 中的 C++ 运算符 ('::'、'->'、...) 处换行

下图显示了我的问题:

在此处输入图片描述

我如何知道\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}

列表

相关内容