3

I often write homework assignments with solutions. I copy the homework tex file and then fill in the solutions. This requires that I keep track of two files. Today I decided to write an environment to wrap my solutions with. The idea is that I can toggle the visibility of this environment to produce the homework and solution PDFs. A nice added feature can be I can change the colour of the solutions to make them pop.

I am currently trying to implement the colour part. Here is what I have,

\NewDocumentEnvironment{sol}{O{blue}}{%
  \leavevmode\color{#1}\ignorespaces%
}{%
}%

Now with something like this,

\begin{sol}
  We proceed by factoring,
  \begin{align*}
    x^2- 8x - 9         & = 9-9         &  & \text{Subtract 9 on both sides.}         \\
    x^2- x + 9x - 9     & = 0           &  & \text{Breaking the middle term.}         \\
    x(x - 1) + 9(x - 1) & = 0           &  & \text{Pulling out common factors.}       \\
    (x - 1)(x + 9)      & = 0           &  & \text{Pulling out common } (x - 1).      \\
    x                   & \in \{1, -9\} &  & f(x)g(x) = 0 \Ra f(x) = 0 \vee g(x) = 0.
  \end{align*}
\end{sol}

I get extra space at the end of the derivation. Could someone point out why and how might I avoid the extraneous space?

8
  • 1
    you should provide an example so people can see the problem and test answers but probably putting \ignorespacesafterend as the end code will do what you want Commented May 18 at 7:19
  • Will that also ignore spaces that were actually written by the author?
    – scribe
    Commented May 18 at 7:21
  • 1
    the problem (I assume) is a space written by the author (that is if the document had \end{sol}% to hide the white space after the environment the extra space would go. Commented May 18 at 7:22
  • 1
    It seems I have this problem.
    – scribe
    Commented May 18 at 8:35
  • 1
    @Mico: my guess is \newcommand\Ra{\Rightarrow}, though the OP probably should just use \implies. Commented May 18 at 12:34

1 Answer 1

4

I get extra space at the end of the derivation. Could someone point out why and how might I avoid the extraneous space?

It's not the fault of the environment definition. Instead, the issue arises because LaTeX is programmed to insert whitespace both above and below a display-math environment, such as align*.

To get rid of the unwanted whitespace, don't use a display-math environment toward the end of the sol environment.

enter image description here

\documentclass{article} % or some other suitable document class
\usepackage{xcolor,amsmath}
\NewDocumentEnvironment{sol}{O{blue}}{\leavevmode\color{#1}}{}

\providecommand\Ra\Rightarrow % ? @WillieWong's suugestion :-)

\begin{document}

\hrule 
\begin{sol}
We proceed by factoring,\strut

\medskip
\centering
$\begin{aligned}
    x^2- 8x - 9         &=9-9         && \text{Subtract 9 on both sides.}     \\
    x^2- x + 9x - 9     &=0           && \text{Break the middle term.}        \\
    x(x - 1) + 9(x - 1) &=0           && \text{Pull out common factors.}      \\
    (x - 1)(x + 9)      &=0           && \text{Pull out common $(x - 1)$.}    \\
    x                   &\in\{1, -9\} && f(x)g(x) = 0 \Ra f(x) = 0 \vee g(x) = 0.
\end{aligned}$ % be sure to leave a blank line below this one

\end{sol}
\hrule

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .