我有以下构造(使用包mathtools
)
\begin{drcases}
Content....
\end{drcases} \parbox{.53\textwidth}{Long Multiline Text Blablabla}
我用cases
它来显示一些长文本,我发现通过将文本放入其中,\parbox
我可以将其分布在多行中。但我最终手动调整了文本宽度(.53\textwidth
)
而且因为我的文档中多次出现这种构造,所以我想知道是否可以自动计算文本宽度,这样我就不需要每次手动执行此操作。
答案1
的一个应用程序(查看2019-03-05 之前xparse
版本的编辑历史)xparse
\documentclass{article}
\usepackage{xparse,mathtools}
\usepackage{lipsum}
\ExplSyntaxOn
\NewDocumentEnvironment{xdrcases}{O{4em}mb}
{
\axmx_xdrcases:nnn { #1 } { #2 } { #3 }
}
{}
\box_new:N \l_axmx_xdrcases_box
\dim_new:N \l_axmx_xdrcases_dim
\cs_new_protected:Npn \axmx_xdrcases:nnn #1 #2 #3
{
\hbox_set:Nn \l_axmx_xdrcases_box
{
$\begin{drcases}#3\end{drcases}$
}
\dim_set:Nn \l_axmx_xdrcases_dim
{
\displaywidth - \box_wd:N \l_axmx_xdrcases_box - #1
}
\box_use:N \l_axmx_xdrcases_box
\quad
\parbox{\l_axmx_xdrcases_dim}{#2}
}
\ExplSyntaxOff
\begin{document}
\renewcommand{\theequation}{1.1.\arabic{equation}} % just to show the problem
\lipsum*[2]
\begin{equation*}
\begin{xdrcases}{
Some long descriptive text, very boring indeed, but
that should be long enough to require splitting it
across lines
}
\frac{1}{2}x & \text{if $x$ is rational} \\
-x & \text{if $x$ is irrational}
\end{xdrcases}
\end{equation*}
\lipsum*[2]
\begin{equation}
\begin{xdrcases}[6em]{
Some long descriptive text, very boring indeed, but
that should be long enough to require splitting it
across lines
}
\frac{1}{2}x & \text{if $x$ is rational} \\
-x & \text{if $x$ is irrational}
\end{xdrcases}
\end{equation}
\lipsum[4]
\end{document}
可选参数(默认 4em)设置留空的空间;如您所见,对于较长的方程式数字,4em 可能太小,因此可以采取相应的措施。
您可能需要检查以下变体,其中要放在侧面的文本位于宏之后\sidecondition
;如果\sidecondition
没有出现,则环境与相同drcases
。
\documentclass{article}
\usepackage{xparse,mathtools}
\usepackage{lipsum}
\ExplSyntaxOn
\NewDocumentEnvironment{xdrcases}{O{4em}b}
{
\axmx_xdrcases:nn { #1 } { #2 }
}
{}
\box_new:N \l_axmx_xdrcases_box
\dim_new:N \l_axmx_xdrcases_dim
\seq_new:N \l_axmx_xdrcases_seq
\cs_new_protected:Npn \axmx_xdrcases:nn #1 #2
{
\seq_set_split:Nnn \l_axmx_xdrcases_seq { \sidecondition } { #2 }
\int_compare:nTF { \seq_count:N \l_axmx_xdrcases_seq > 1 }
{% there is a side condition
\hbox_set:Nn \l_axmx_xdrcases_box
{
$\begin{drcases}\seq_item:Nn \l_axmx_xdrcases_seq { 1 }\end{drcases}$
}
\dim_set:Nn \l_axmx_xdrcases_dim
{
\displaywidth - \box_wd:N \l_axmx_xdrcases_box - #1
}
\box_use:N \l_axmx_xdrcases_box
\quad
\parbox{\l_axmx_xdrcases_dim}{\seq_item:Nn \l_axmx_xdrcases_seq { 2 }}
}
{% no side condition
\begin{drcases}#2\end{drcases}
}
}
\ExplSyntaxOff
\begin{document}
\renewcommand{\theequation}{1.1.\arabic{equation}} % just to show the problem
\lipsum*[2]
\begin{equation*}
\begin{xdrcases}
\frac{1}{2}x & \text{if $x$ is rational} \\
-x & \text{if $x$ is irrational}
\sidecondition
Some long descriptive text, very boring indeed, but
that should be long enough to require splitting it
across lines
\end{xdrcases}
\end{equation*}
\lipsum*[2]
\begin{equation}
\begin{xdrcases}[6em]
\frac{1}{2}x & \text{if $x$ is rational} \\
-x & \text{if $x$ is irrational}
\sidecondition
Some long descriptive text, very boring indeed, but
that should be long enough to require splitting it
across lines
\end{xdrcases}
\end{equation}
\lipsum*[4]
\begin{equation*}
\begin{xdrcases}
\frac{1}{2}x & \text{if $x$ is rational} \\
-x & \text{if $x$ is irrational}
\end{xdrcases}
\end{equation*}
\lipsum[3]
\end{document}