# Problem with spacing after $…$ environment when using the \textcolor{} command

I'm getting odd spacing/indentation behavior when the \textcolor command is combined with the $...$ environment. I did find a fix by putting % in a couple places, as outlined below, but I'd like to know if anyone has a better solution. The picture below, followed by a MWE, explains the problem in more detail.

Output:

Code:

\documentclass[a4paper,11pt]{article}
\usepackage{amsmath, amsfonts, amssymb, parskip, dsfont, amsthm, wasysym, mathrsfs}
\usepackage{graphicx}
\usepackage[usenames, dvipsnames]{xcolor}
\usepackage[top=1in, bottom=1in, left=1in, right=1in]{geometry}
\usepackage{mathrsfs}
\usepackage[normalem]{ulem}
\usepackage{soul, color} % for highlighting
\usepackage{pifont} % for cool symbols in text mode

\begin{document}

\textbf{No problems with spacing or indents when I don't use the
\textbackslash[ ... \textbackslash] environment.}

\textcolor{NavyBlue}{Insert problem description here.}

Insert problem solution here.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\textbf{When I use the \textbackslash[ ... \textbackslash] environment, it
puts a large space after
the \textbackslash textcolor section.}

Insert problem solution here. %% this line should have been removed

\textcolor{NavyBlue}{Insert problem description here. Along with some math:
$1 \neq 0$}

Insert problem solution here.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\textbf{I can sort of fix the problem by eliminating the space
between the problem statement and the solution statement, but
then there is an ugly little indent.}

\textcolor{NavyBlue}{Insert problem description here. Along with some math:
$1 \neq 0$}
Insert problem solution here.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\textbf{Finally, I realized that I could fix'' the problem by putting a
comment character at the end of the line and between the lines.}

\textcolor{NavyBlue}{Insert problem description here. Along with some math:
$1 \neq 0$}%
%
Insert problem solution here.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\textbf{But this seems incredibly hacky, and I'm hoping someone can point
me to a more elegant fix.}

\end{document}


EDIT: So after using your suggestions (Johannes), it no longer has the indent, but it that space is still there. Here's what I'm talking about:

Code:

Output:

EDIT #2: It's a very minor difference, but I'm seeing a larger gap between the math and the text than between the text and the text:

Here's the relevant code:

\textbf{4.} \hspace{5 pt}
\begin{specialmathtwo}
Blah blah blah blah blah blah blah blah blah blah Turing machine blah blah such that
$L \subseteq \{ M \mid \text{M is a Turing machine that blah blah blah} \}.$
\end{specialmathtwo}
Blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah BLAH blah blah blah blah blah blah blah blah blah.

Blah blah blah blah blah blah blah.


and

\newenvironment{specialmathtwo}{
\color{NavyBlue}
}{%
\par %Maybe you want to finish all of them off with a paragraph
}

-
Btw: good minimal example –  Johannes_B Mar 31 '14 at 7:56
Note that the option is a4paper without spaces; it works with spaces, but just by chance. Also you shouldn't specify the pdftex option to graphicx. –  egreg Mar 31 '14 at 8:53
Thanks, @egreg, I made those changes. –  AmadeusDrZaius Mar 31 '14 at 9:08

Better not to use \textcolor along with maths over multiple line. You can use \color{<colorname>} instead, and limit the scope by grouping. To be honest, \textcolor does grouping as well, but starts \leavevmode to get to horizontal mode, which makes a little mess for your example.

You can also define a new environment for the colors. That keeps the advantage of having everything in more semantic way. Meaning: What is shown, not how it is shown.

The extra amount of white space is caused by parskip. Parskips are ugly anyways.
The little indentation you noticed is caused by a space, that came from the \textcolor-grouping. \endgroup is hiding that space. Thanks @DavidCarlisle.

\documentclass[a4paper,11pt]{article}
\usepackage{ parskip }
\usepackage{ blindtext }
\usepackage[usenames, dvipsnames]{xcolor}
\usepackage{showframe}
\newenvironment{specialmath}{
\color{NavyBlue}
}{%
\par %Maybe you want to finish all of them off with a paragraph
}
\begin{document}

\begingroup
\color{NavyBlue}
Insert problem description here. Along with some math:
Insert problem description here. Along with some math:
Insert problem description here. Along with some math:
$1 \neq 0$
\endgroup
\blindtext

\blindtext
%just a visual separation, but no \par
\begin{specialmath}
$\sum \int \sin 0 = a$
A little descriptive text
\end{specialmath}
\blindtext

\end{document}


That is giving me this output:

-
Perhaps elaborate that \textcolor is really for 'text' (horizontal mode material: if features \leavevmode), and so it's fundamentally wrong for display math. –  Joseph Wright Mar 31 '14 at 7:51
No, it's not the \color \textcolor difference. \textcolor starts with \leavevmode but the start of the coloured section isn't the problem. The problem is at the end where there was a space after the } by using \endgroup you avoid the issue a there is no space token after a control sequence name. –  David Carlisle Mar 31 '14 at 8:14
@AmadeusDrZaius never use blank lines in the source for "readability" a blank line is a \par instruction and will change the layout in lots of ways. –  David Carlisle Mar 31 '14 at 8:15
@AmadeusDrZaius Comment out package parskip and test with and without a blank line. The difference is more obvious. –  Johannes_B Mar 31 '14 at 8:17
@AmadeusDrZaius tex is mostly designed for typesetting text and in natural language text white space is usually significant so the white space rules in tex are not at all like those in most programming languages. –  David Carlisle Mar 31 '14 at 8:26