我有以下在环境\dotfill
中使用的环境cases
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tabularx}
\setlength{\parindent}{0pt}
\makeatletter
\newenvironment{DotCases}[1]{%
{#1}
\@ifdefinable\@FunFill{\newdimen\@FunFill}
\settowidth\@FunFill{$#1\left\{ \right.\kern -\nulldelimiterspace$}
\left\{%
\tabcolsep \z@
\def\arraystretch{1.2}
\tabularx{\dimexpr \linewidth-\@FunFill-2pt \relax}%
{>{$}r<{$}>{${}}X<{$}}%
}{%
\endtabularx
\right.%
\kern -\nulldelimiterspace
}
\makeatother
\begin{document}
$
\begin{DotCases}{f(x) = }
& x \dotfill \text{text}\\
& 1-x \dotfill \text{more text}
\end{DotCases}
$
$
\begin{DotCases}{f(x) = }
& x \dotfill \text{text}\\
& 1-x \dotfill \text{more text}
\end{DotCases}
$
\end{document}
我从各个地方收集了解决方案,但我只部分理解了发生了什么。我试图将所有内容保持在命令内,从而从命令中减去左括号linewidth
的宽度- 嗯,因为我必须额外减去大约 2pt 才能确保垂直对齐。#1
\dotfill
编写单个DotCases
环境可以正常工作,但编写多个环境DotCases
会出现错误\@FunFill already defined
。有什么建议可以解决这个问题吗?
答案1
永远不要在文档环境中分配寄存器,它们是固定资源,您只需要在代码加载时分配它们一次。
\documentclass[a4paper]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{tabularx}
\setlength{\parindent}{0pt}
\makeatletter
\newdimen\@FunFill
\newenvironment{DotCases}[1]{%
{#1}%%%%%%
\settowidth\@FunFill{$#1\left\{ \right.\kern -\nulldelimiterspace$}%%%%%%%
\left\{%
\tabcolsep \z@
\def\arraystretch{1.2}
\tabularx{\dimexpr \linewidth-\@FunFill-2pt \relax}%
{>{$}r<{$}>{${}}X<{$}}%
}{%
\endtabularx
\right.%
\kern -\nulldelimiterspace
}
\makeatother
\begin{document}
$
\begin{DotCases}{f(x) = }
& x \dotfill \text{text}\\
& 1-x \dotfill \text{more text}
\end{DotCases}
$
$
\begin{DotCases}{f(x) = }
& x \dotfill \text{text}\\
& 1-x \dotfill \text{more text}
\end{DotCases}
$
\end{document}