newcommand 在文本前放置奇怪的空格

newcommand 在文本前放置奇怪的空格

我正在尝试按照通常的方式“制作一个带有空白的工作表”,我需要比文本更长的空白,这些空白最终会填满它们(以便学生可以手写答案)。不幸的是,我的命令“空白”在文本中求值之前打印了大量奇怪的空格。如果我在正文中插入相同的命令,它会正常求值。

    \documentclass[]{article}
    \newlength\mylen
    \newlength\fieldlen
    \newcommand\blank[1]{%
       \settowidth\mylen{#1}
       \settowidth\fieldlen{0}
       \addtolength\fieldlen{\mylen}
       \addtolength\fieldlen{\mylen}
       \underline{\makebox[\fieldlen]{}}    %comment this line and uncomment next line to hide answers
       %\underline{\makebox[\fieldlen]{#1}} %comment this line and uncomment previous line to show answers
    }
    \begin{document}

Using the macro, I get the following:

We find that $G<$\blank{$G_1$}.

There's an extra space before the underline. 

I want it to look like:

We find that $G<$\underline{\makebox[\fieldlen]{}}.
\end{document}

答案1

宏的使用\settowidth和在末尾\addtolength缺少一个,因此缺少 4 个就会导致每个宏多一个空格字符。%%

%另外,后面缺少一个\underline,所以.向右移动了。

  \documentclass[]{article}
  \newlength\mylen
  \newlength\fieldlen
  \newcommand\blank[1]{%
    \settowidth{\mylen}{#1}%
    \settowidth{\fieldlen}{0}%
    \addtolength{\fieldlen}{\mylen}%
    \addtolength{\fieldlen}{\mylen}%
    \underline{\makebox[\fieldlen]{}}%    %comment this line and uncomment next line to hide answers
    % \underline{\makebox[\fieldlen]{#1}} %comment this line and uncomment previous line to show answers
  }
  \begin{document}

Using the macro, I get the following:

We find that $G<$\blank{$G_1$}.

There's an extra space before the underline. 

I want it to look like:

We find that $G<$\underline{\makebox[\fieldlen]{}}.
\end{document}

在此处输入图片描述

相关内容