def 生成的表格周围不需要的空间

def 生成的表格周围不需要的空间

我在用着我找到的一些代码通过简单的输入生成一个格式良好的方程式求解步骤列表。但是,我的方法是在结果的左侧和底部添加不必要的空格。请看:

\documentclass{article}

\usepackage{xstring} % using: StrSubstitute

% equation solving list
\renewcommand\equation[1]{
\everymath{\displaystyle}
\renewcommand{\arraystretch}{2.5}
\begin{tabular}{@{}r@{}c@{}l@{}}
\foreach{\makeeq}{}{#1}
\end{tabular}\\
}

% equation helper function (first parameter is unused)
\def\makeeq#1#2{
\saveexpandmode\expandarg
\StrSubstitute[1]{#2}{=}{$&$\ =\ $&$}[\subbed]
\restoreexpandmode
\subbed\\
}

% % % FOREACH % % %

\makeatletter

% Functional foreach construct 
% #1 - Function to call on each comma-separated item in #3
% #2 - Parameter to pass to function in #1 as first parameter
% #3 - Comma-separated list of items to pass as second parameter to function #1
\def\foreach#1#2#3{%
\@test@foreach{#1}{#2}#3,\@end@token%
}

% Internal helper function - Eats one input
\def\@swallow#1{}

% Internal helper function - Checks the next character after #1 and #2 and 
% continues loop iteration if \@end@token is not found 
\def\@test@foreach#1#2{%
\@ifnextchar\@end@token%
 {\@swallow}%
 {\@foreach{#1}{#2}}%
}

% Internal helper function - Calls #1{#2}{#3} and recurses
% The magic of splitting the third parameter occurs in the pattern matching of the \def
\def\@foreach#1#2#3,#4\@end@token{%
#1{#2}{#3}%
\@test@foreach{#1}{#2}#4\@end@token%
}

\makeatother

% % % % % %

\begin{document}

desired result: \
start

\everymath{\displaystyle}
\renewcommand{\arraystretch}{2.5}
\begin{tabular}{@{}r@{}c@{}l@{}}
$ X $&$\ =\ $&$ 1 + 1 $ \\
$ $&$\ =\ $&$ 2 $ \\
\end{tabular}\
end

\bigskip
actual result: \
start

\equation{
$ X = 1 + 1 $,
$ = 2 $
}

end

\end{document}

输出:

结果

我不知道为什么会添加空格。如能提供任何帮助,我将不胜感激。谢谢!

答案1

这里的具体问题是您的宏引入了一些额外的空格。LaTeX 有时会将换行符解释为空格,因此通过用该%字符结束行,您可以阻止这些空格被引入。对于您的情况,请\makeeq像这样修复您的宏以获得所需的输出:

% equation helper function (first parameter is unused)
\def\makeeq#1#2{%  <--- added
\saveexpandmode\expandarg
\StrSubstitute[1]{#2}{=}{$&$\ =\ $&$}[\subbed]%    <--- added
\restoreexpandmode
\subbed\\
}

答案2

不需要的垂直空间来自\\的定义中的\makeeq,当 for-each 循环结束时,它将插入一个“无”作为表格的第三行。您应该考虑从定义中\@swallow删除并改为写。\\\equation{$ X = 1 + 1 $\\,$ = 2 $}

相关内容