1

I'm using some code I found to generate, from simple input, a nicely formatted list of steps in solving an equation. However, my method is adding unwanted space to the left and at the bottom of the result. Please have a look:

\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}

The output:

result

I can't figure out what's adding the whitespace. Any help would be appreciated. Thanks!

4
  • Can you elaborate on the purpose a little more? The code, with no offense, uses some outdated solutions for the constructs as far as I can see. If you can instead turn your question more towards what you would like to achieve I think there would be more fruitful answers here.
    – percusse
    Sep 14, 2013 at 18:02
  • Welcome to TeX.SX! It's a bad idea to do \renewcommand{\equation} to begin with. Choose another name, because equation is a very important environment and it may come useful for you.
    – egreg
    Sep 14, 2013 at 18:05
  • Thanks for your replies, and the welcome! percusse: No offense taken! What I would like to achieve can mostly be seen in the image I posted, as the 'desired result'. I would like to create a command with an argument where I specify steps in the equation solving process. The output then lists these steps, with the first step aligned to the left, and subsequent steps aligned by the first equals signs in each of them. Steps may contain multiple equals signs - the first of which should be aligned. This should output this.
    – user36693
    Sep 14, 2013 at 18:28
  • Whoops, that first link should be this. The f(x) is to show the desired behaviour of differing lengths of the left parts of the equations.
    – user36693
    Sep 14, 2013 at 18:46

2 Answers 2

0

The particular problem here is that your macros are introducing some extra spaces. LaTeX sometimes interprets newlines as spaces, and so by ending lines with the % character, you can stop these being introduced. In your case, fix your \makeeq macro like so to get your desired output:

% equation helper function (first parameter is unused)
\def\makeeq#1#2{%  <--- added
\saveexpandmode\expandarg
\StrSubstitute[1]{#2}{=}{$&$\ =\ $&$}[\subbed]%    <--- added
\restoreexpandmode
\subbed\\
}
1
  • Thanks for your reply! The added % characters solve the offset on the left. However, there is still whitespace below the table. Would you know a solution to that, too?
    – user36693
    Sep 14, 2013 at 18:26
0

The unwanted vertical space comes from the \\ in the definition of \makeeq, that when the for-each loop is ended, \@swallow will insert a "nothing" as the third row of your table. You should consider taking out the \\ from the definition and write \equation{$ X = 1 + 1 $\\,$ = 2 $} instead.

1
  • Thanks, that makes everything look exactly as I want it, and I see why. Since I will always want to add that \` at the end of each line though; is there another solution to bring it back into my macro definition? Could I do a check in \makeeq` such as this? if \subbed is empty {do nothing} else {\subbed\\}
    – user36693
    Sep 14, 2013 at 19:29

You must log in to answer this question.