文本命令中的空格太多

文本命令中的空格太多
\documentclass[11pt]{article}
\usepackage{xcolor}
\newcommand{\textdef}[1]
{
{\color{blue}\textbf{#1}}
}
\begin{document}

An integer is \textdef{even} if it is divisible by $2$.

An integer is {\color{blue}\textbf{odd}} if it is not divisble by $2$.

\end{document}

在上面的代码中,两行的定义方式几乎相同,但第一行中单词“even”周围似乎多了一些空格。这些多余空格是从哪里来的?我该如何去掉它?

间距问题

答案1

如果没有 保护,结束行将被视为空格%。最好使用\textcolor{blue}{text}而不是{\color{blue}text}

\documentclass[11pt]{article}
\usepackage{xcolor}
\newcommand{\textdef}[1]
{                                 <--- one space is here
{\color{blue}\textbf{#1}}         <--- one space is here
}
\begin{document}

An integer is \textdef{even} if it is divisible by $2$.

An integer is {\color{blue}\textbf{odd}} if it is not divisble by $2$.

\end{document}

正确代码:

\documentclass[11pt]{article}
\usepackage{xcolor}

\newcommand{\textdef}[1]{%
  \textcolor{blue}{\textbf{#1}}%
}

\begin{document}

An integer is \textdef{even} if it is divisible by $2$.

An integer is \textcolor{blue}{\textbf{odd}} if it is not divisble by $2$.

\end{document}

在此处输入图片描述

相关内容