文本在左边距居中对齐;神秘的水平空间

文本在左边距居中对齐;神秘的水平空间

我有以下内容:

\documentclass{article}
\usepackage{calc, expl3, xparse}

\makeatletter\ExplSyntaxOn
\def\globalsettoheight{\@gsettodim\ht}
\def\globalsettodepth {\@gsettodim\dp}
\def\globalsettowidth {\@gsettodim\wd}

\def\@gsettodim#1#2#3{%
  \setbox\@tempboxa\hbox{{#3}}\global#2#1\@tempboxa
  \setbox\@tempboxa\box\voidb@x}

\newlength\length@interviewer
\newlength\length@interviewee
\newlength\length@sep

\settowidth\length@sep{:\hspace{1em}}

\NewDocumentCommand\interviewer { m m } {
  \globalsettowidth   \length@interviewer { #1 }

  % Display the full name and then globally redefine to initials,
  % updating the length accordingly.
  \def                      \@interviewer {
    #1
    \global\def             \@interviewer { #2 }
    \globalsettowidth \length@interviewer { #2 }
  }
}

\NewDocumentCommand\interviewee { m m } {
  \globalsettowidth   \length@interviewee { #1 }

  % Display the full name and then globally redefine to initials,
  % updating the length accordingly.
  \def                      \@interviewee {
    #1
    \global\def             \@interviewee { #2 }
    \globalsettowidth \length@interviewee { #2 }
  }
}

\NewDocumentEnvironment { question } { m } {
  \par\noindent
  \hspace{-\length@interviewer}
  \hspace{-\length@sep}
  \textsc{\@interviewer}:
  \hspace{1em}
  #1
  \vspace{1ex}

  \par\noindent
  \hspace{-\length@interviewee}
  \hspace{-\length@sep}
  \textsc{\@interviewee}:
  \hspace{1em}
  \ignorespaces
} {
  \vspace{3ex}
}
\ExplSyntaxOff\makeatother

\interviewer{Jane Doe}{JD}
\interviewee{John Smith}{JS}

\setlength\parindent{3em}

\usepackage{mwe}
\begin{document}
\begin{question}{Sample Question?}
  \lipsum[1]
\end{question}
\begin{question}{Sample Question?}
  \lipsum[2]
\end{question}
\begin{question}{Sample Question?}
  \lipsum[3]
\end{question}
\end{document}

输出结果如下:

输出

为什么第一次使用时没有对齐?多余的空间从哪里来的?(expl3如果觉得刺眼,可以随意在代码中添加 -ify —— 我不得不对其进行调整,使其更具可读性,以便我可以尝试找出这个问题。)

答案1

您存储的是采访者和受访者的未格式化版本,但您\textsc在文档中设置了格式化版本(使用)。您还使用这个未格式化的版本来测量长度,这导致了错位。

\interviewer我已经通过使用以下和的定义解决了这个问题\interviewee

\NewDocumentCommand\interviewer { m m } {
  \globalsettowidth   \length@interviewer { \scshape #1 }% Updated with formatting

  % Display the full name and then globally redefine to initials,
  % updating the length accordingly.
  \def                      \@interviewer {
    #1
    \global\def             \@interviewer { #2 }
    \globalsettowidth \length@interviewer { #2 }
  }
}

\NewDocumentCommand\interviewee { m m } {
  \globalsettowidth   \length@interviewee { \scshape #1 }% Updated with formatting

  % Display the full name and then globally redefine to initials,
  % updating the length accordingly.
  \def                      \@interviewee {
    #1
    \global\def             \@interviewee { #2 }
    \globalsettowidth \length@interviewee { #2 }
  }
}

在此处输入图片描述

不过,我建议定义一个合适的宏,分别存储采访者和被采访者的格式。例如,

\providecommand{\@interviewerfont}{\normalfont}
\providecommand{\@intervieweefont}{\normalfont}
\newcommand{\interviewerfont}[1]{%
  \renewcommand{\@interviewerfont}{#1}}
\newcommand{\intervieweefont}[1]{%
  \renewcommand{\@intervieweefont}{#1}}

然后将它们放在\interviewer\interviewee定义中的适当位置。

相关内容