使用 \textcolor{} 命令时,\[...\] 环境后的间距问题

使用 \textcolor{} 命令时,\[...\] 环境后的间距问题

当 \textcolor 命令与 \[ ... \] 环境结合使用时,我得到了奇怪的间距/缩进行为。我确实找到了一个解决方法,方法是放入%几个地方,如下所述,但我想知道是否有人有更好的解决方案。下面的图片,后面是 MWE,更详细地解释了这个问题。

输出:

在此处输入图片描述

代码:

\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[breaklinks=true]{hyperref}
\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}

编辑:因此,在使用您的建议(Johannes)后,它不再有缩进,但那个空格仍然存在。这就是我要说的:

代码

在此处输入图片描述

输出

在此处输入图片描述

编辑#2:这是一个非常小的差异,但我发现数学和文本之间的差距比文本和文本之间的差距更大:

在此处输入图片描述

相关代码如下:

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

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

答案1

最好不要\textcolor在多行上使用数学运算。您可以改用\color{<colorname>},并通过分组来限制范围。说实话,\textcolor分组也很好,但开始\leavevmode进入水平模式,这会给您的示例带来一些混乱。

您还可以为颜色定义一个新的环境。这样可以保留以更语义的方式呈现所有内容的优势。含义:显示什么,而不是如何显示。

多余的空白是由 parskip 造成的。Parskip 是丑陋的无论如何。
您注意到的小缩进是由空格引起的,该空格来自 -grouping \textcolor\endgroup隐藏了该空格。谢谢@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}

这给了我这个输出:

示例的输出

相关内容