我想要内联数学文本,例如
Figure \ref{figure:example graph} shows an example of a graph $G = (\{v_{1}, \dots, v_{5}\}, \{(v_{1}, v_{2}), (v_{2}, v_{3}), (v_{1}, v_{3}), (v_{2}, v_{5}), (v_{3}, v_{4}), (v_{4}, v_{5})\})$ with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
但是,LaTeX 会弄乱换行符,并且渲染后的文本看起来就像所附的屏幕截图中那样。
有什么办法可以让我告诉 LaTeX 在简单的 $ 内分解公式吗?
答案1
解决这个问题最简单的方法是定义一个可断开逗号的新命令:
\documentclass{article}
\newcommand{\bcomma}{,\allowbreak}
\begin{document}
Figure \ref{figure:example graph} shows an example of a graph
$G = (\{v_{1}, \dots, v_{5}\},
\{(v_{1}, v_{2})\bcomma
(v_{2}, v_{3})\bcomma
(v_{1}, v_{3})\bcomma
(v_{2}, v_{5})\bcomma
(v_{3}, v_{4}),
(v_{4}, v_{5})\})$
with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
\end{document}
我宁愿不在\bcomma
顶点列表之后和最后一条边之前使用,但在紧急情况下您也可以更改它们。
可以使用 LaTeX3 工具编写更复杂的解决方案:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mathlist}{ O{,} m m }
{
\egreg_mathlist:nnn { #1 } { #2 } { #3 }
}
\seq_new:N \l__egreg_mathlist_seq
\cs_new_protected:Npn \egreg_mathlist:nnn #1 #2 #3
{
\seq_set_split:Nnn \l__egreg_mathlist_seq { #1 } { #3 }
\seq_use:Nnnn \l__egreg_mathlist_seq { #2 } { #2 } { #2 }
}
\ExplSyntaxOff
\begin{document}
Figure \ref{figure:example graph} shows an example of a graph
$G = (\{\mathlist{,}{v_{1}, \dots, v_{5}}\},
\{\mathlist[;]{,\allowbreak}
{(v_{1}, v_{2});
(v_{2}, v_{3});
(v_{1}, v_{3});
(v_{2}, v_{5});
(v_{3}, v_{4});
(v_{4}, v_{5})}
\})$
with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
\end{document}
该\mathlist
命令有一个可选参数(项目分隔符,默认为逗号);第一个强制参数告诉用什么来代替分隔符,第二个参数是列表。
\mathlist
示例中的第一个用法当然是多余的;第二个用法则不是:由于在有序对中使用了逗号,因此我选择分号作为分隔符,并在处理后用分号替换,,\allowbreak
如上面的简单定义所示。
如果边列表存储在宏中,则上述方法不起作用。但可以扩展定义以应对这种情况:
\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mathlist}{ s O{,} m m }
{
\IfBooleanTF{#1}
{ \egreg_mathlist:nnV { #2 } { #3 } #4 }
{ \egreg_mathlist:nnn { #2 } { #3 } { #4 } }
}
\seq_new:N \l__egreg_mathlist_seq
\cs_new_protected:Npn \egreg_mathlist:nnn #1 #2 #3
{
\seq_set_split:Nnn \l__egreg_mathlist_seq { #1 } { #3 }
\seq_use:Nnnn \l__egreg_mathlist_seq { #2 } { #2 } { #2 }
}
\cs_generate_variant:Nn \egreg_mathlist:nnn { nnV }
\ExplSyntaxOff
\newcommand{\edgelist}{(v_{1}, v_{2}); (v_{2}, v_{3}); (v_{1}, v_{3});
(v_{2}, v_{5}); (v_{3}, v_{4}); (v_{4}, v_{5})}
\begin{document}
Figure \ref{figure:example graph} shows an example of a graph
$G = (\{\mathlist{,}{v_{1}, \dots, v_{5}}\},
\{\mathlist[;]{,\allowbreak}
{(v_{1}, v_{2});
(v_{2}, v_{3});
(v_{1}, v_{3});
(v_{2}, v_{5});
(v_{3}, v_{4});
(v_{4}, v_{5})}
\})$
with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
The list of edges can also be obtained by
$\mathlist*[;]{,\allowbreak}{\edgelist}$.
\end{document}
答案2
这最簡單实现你想要的方法是:
\documentclass{article}
\begin{document}
Figure \ref{figure:example graph} shows an example of a graph
$G = (\{v_{1}, \dots, v_{5}\}$,
$\{(v_{1}, v_{2})$, $(v_{2}, v_{3})$, $(v_{1}, v_{3})$, $(v_{2}, v_{5})$,
$(v_{3}, v_{4})$, $(v_{4}, v_{5})\})$
with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
\end{document}
答案3
您必须告诉 LaTeX 允许在(例如)处中断,
。一种选择是提供一个列表解析器,在每个处插入一个可中断点,
。此版本的\mathlist
:
\documentclass{article}
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
% https://groups.google.com/d/msg/comp.text.tex/PWt75pamgO8/Btouz_IsZq4J
\newcommand{\mathlist}[2][,]{% \mathlist[<separator>]{<csv list>}
\begingroup
% count the elements
\@tempcnta=\z@
\renewcommand*\do[1]{\advance\@tempcnta\@ne}
\docsvlist{#2}% Process list first time
\chardef\@listsize\@tempcnta
\@tempcnta=\@ne
\renewcommand*\do[1]{
\ifnum\@tempcnta<\@listsize
##1#1\penalty\z@ \advance\@tempcnta\@ne
\else
\unpenalty\penalty\@M ##1
\fi}
\docsvlist{#2}% Process list second time
\endgroup
}
\makeatother\begin{document}
Figure~1 shows an example of a graph $G = (\{v_{1}, \dotsc, v_{5}\},
\{(v_{1}, v_{2}), (v_{2}, v_{3}), (v_{1}, v_{3}), (v_{2}, v_{5}),
(v_{3}, v_{4}), (v_{4}, v_{5})\})$ with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
Figure~1 shows an example of a graph $G = (\mathlist{\{v_{1}, \dotsc, v_{5}\}},
\{\mathlist{(v_{1}, v_{2}), (v_{2}, v_{3}), (v_{1}, v_{3}), (v_{2}, v_{5}),
(v_{3}, v_{4}), (v_{4}, v_{5})}\})$ with $|V| = n = 5$ vertices and $|E| = m = 6$ edges.
\end{document}
\mathlist
解析逗号分隔列表两次。第一次查找项目数,第二次设置项目数;这次在后面使用零惩罚,
。最后一项不允许中断。
如果您不关心最后一个项目是否易碎,您可以使用以下(稍短的)定义\mathlist
:
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox
\makeatletter
\newcommand{\mathlist}[2][,]{% \mathlist[<separator>]{<csv list>}
\begingroup
\def\@temp{\penalty\z@}% Put penalty in single token
\toks@={\@gobble}% Gobble first separator
\renewcommand*\do[1]{\toks@=\@xp{\the\toks@#1\@temp##1}}
\docsvlist{#2}% Process list
\the\toks@% Print list
\endgroup
}
\makeatother
etoolbox
提供列表解析器\docsvlist
,同时amsmath
提供\@xp
(\expandafter
)和\dotsc
(表示“逗号点”)。
\mathlist
可以提供一个可选的第一个参数,将输出从默认值更改,
为您提供的任何内容。