发言人姓名的数值增量

发言人姓名的数值增量

我在用这个答案制作一份抄本。我想每次介绍演讲者时都增加,即 Speaker01_01、Speaker02_01、Speaker01_02、Speaker02_02 等等。我想使用某种计数器,但不知道该如何调整代码。MWE 来自上一个答案,而不是我自己的工作。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\usepackage{enumitem}
\setlist[description]{
  font={\sffamily\bfseries},
  labelsep=0pt,
  labelwidth=\transcriptlen,
  leftmargin=\transcriptlen,
}

\newlength{\transcriptlen}

\NewDocumentCommand {\setspeaker} { mo } {%
  \IfNoValueTF{#2}
  {\expandafter\newcommand\csname#1\endcsname{\item[#1:]}}%
  {\expandafter\newcommand\csname#1\endcsname{\item[#2:]}}%
  \IfNoValueTF{#2}
  {\settowidth{\transcriptlen}{#1}}%
  {\settowidth{\transcriptlen}{#2}}%
}

% Easiest to put the longest name last...
\setspeaker{Bob}
\setspeaker{Billy}[Billy Bob]
\setspeaker{xxx}[Xavier Xanadu III]

% How much of a gap between speakers and text?
\addtolength{\transcriptlen}{1em}%

\begin{document}
\begin{description}

  \Bob I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \Billy I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \xxx Perhaps something that aligns the names to the text, and uses a
  columnar layout for the people vs what they said.

\end{description}
\end{document}

这就是我想要实现的目标:

Speaker01_01:Blah Blah Blah
Speaker02_01:Blah Blah Blah
Speaker01_02:Blah Blah Blah
Speaker02_02:Blah Blah Blah

因此:{speakername}{appendnumber}:{说的话}


使用答案,我在 10 处得到了一个多余的空格。

在此处输入图片描述

答案1

我对你的代码做了一些修改,最明显的是你不必再把最长的名字放在最后(会检查哪个名字是最长的)。

第一次运行时扬声器的宽度等于最长扬声器的宽度加上 0 所需的宽度以及扬声器和其文本之间的跳跃。在连续运行时,也会考虑最大数字,因此宽度设置为所需的大小。不过,您可以设置长度的上限\speakerwidthmax(我在这里将其设置为 4 厘米)。

编辑:\resetspeakers我添加了将所有计数器重置为 0 的宏。

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\usepackage{enumitem}
\setlist[description]{
  font=\speakerfont,
  labelsep=0pt,
  labelwidth=\speakerwidth,
  leftmargin=\speakerwidth,
}

\newlength{\speakerlongest}
\newlength{\speakerskip}
\newlength{\speakerwidthmax}

% distance between speakers and text
\setlength{\speakerskip}{1em}
% maximum width for speaker names
\setlength{\speakerwidthmax}{4cm}

\newcommand*\speakerfont{\sffamily\bfseries}
\newcommand*\speakerwidth
  {%
    \ifdim\speakerlongest>\speakerwidthmax
      \speakerwidthmax
    \else
      \speakerlongest
    \fi
  }
\newcommand*\printscriptline[2]
  {%
    \begingroup
    \setbox0\hbox{#2, \csname the#1count\endcsname:\hskip\speakerskip}%
    \usebox0%
    \ifdim\speakerlongest<\wd0
      \global\setlength\speakerlongest{\wd0}%
    \fi
    \endgroup
  }
\newcommand*\speakerlongestset[1]
  {%
    \begingroup
    \settowidth{\dimen0}{\speakerfont#1, 0:\hskip\speakerskip}%
    \expandafter\endgroup
    \expandafter\speakerlongestsetAUX\expandafter{\the\dimen0}%
  }
\newcommand*\speakerlongestsetAUX[1]
  {%
    \ifdim\speakerlongest<#1
      \setlength\speakerlongest{#1}%
    \fi
  }
\makeatletter
\AtEndDocument
  {%
    \write\@auxout
      {\global\protect\setlength\protect\speakerlongest{\the\speakerlongest}}%
  }
\newcommand*\resetspeakers
  {%
    \begingroup
    \@for\speaker:=\speakerslist\do{\setcounter{\speaker count}{0}}%
    \endgroup
  }
\let\speakerslist\empty
\makeatother

\NewDocumentCommand {\setspeaker} { mo } {%
  \newcounter{#1count}%
  \ifx\speakerslist\empty
    \def\speakerslist{#1}%
  \else
    \edef\speakerslist{\unexpanded\expandafter{\speakerslist,#1}}%
  \fi
  \IfNoValueTF{#2}
    {%
      \expandafter\newcommand\csname#1\endcsname
        {%
          \refstepcounter{#1count}%
          \item[\printscriptline{#1}{#1}]%
        }%
      \speakerlongestset{#1}%
    }%
    {%
      \expandafter\newcommand\csname#1\endcsname
        {%
          \refstepcounter{#1count}%
          \item[\printscriptline{#1}{#2}]%
        }%
      \speakerlongestset{#2}%
    }%
}

% Easiest to put the longest name last...
\setspeaker{xxx}[Xavier Xanadu III]
\setspeaker{Billy}[Billy Bob]
\setspeaker{Bob}

\begin{document}
\begin{description}

  \Bob I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \Billy I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \xxx Perhaps something that aligns the names to the text, and uses a
  columnar layout for the people vs what they said.

  \Bob This is my second line!

  \resetspeakers
  \xxx This is my second line!
  \xxx This is my second line!
  \xxx This is my second line!
  \xxx This is my second line!
  \xxx This is my second line!
  \xxx This is my second line!
  \xxx This is my second line!
  \xxx This is my second line!
  \xxx This is my second line!
\end{description}
\end{document}

在此处输入图片描述

答案2

标签宽度可以在文档末尾设置,您只需重新运行 LaTeX 即可获得正确效果(如果需要,会发出警告):

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}
\usepackage{enumitem}

\setlist[description]{
  font={\sffamily\bfseries},
  labelsep=0pt,
  labelwidth=\transcriptlen,
  leftmargin=\transcriptlen,
}

\newlength{\transcriptlen}

\ExplSyntaxOn

\NewDocumentCommand{\setspeaker}{ mO{#1} }
 {% if the optional argument is missing, default it to the first argument
  \zafka_setspeaker:nn { #1 } { #2 }
 }

\dim_new:N \l_zafka_width_dim
\dim_new:N \g_zafka_width_dim

\cs_new_protected:Nn \zafka_setspeaker:nn
 {
  % allocate a new counter
  \int_new:c { g_zafka_#1_int }
  % define a command
  \cs_new_protected:cpn {#1}
   {
    % increase the counter
    \int_gincr:c { g_zafka_#1_int }
    % measure the label width
    \settowidth{\l_zafka_width_dim}{{\sffamily\bfseries #2,~\int_use:c { g_zafka_#1_int }}\quad}
    % update the maximum width
    \dim_gset:Nn \g_zafka_width_dim
     {
      \dim_max:nn { \g_zafka_width_dim } { \l_zafka_width_dim }
     }
    % add the item
    \item[#2,~\int_use:c { g_zafka_#1_int }]
   }
 }

\AtEndDocument
 {
  % if the maximum width is different from \transcriptlen, issue a warning
  \dim_compare:nF { \transcriptlen = \g_zafka_width_dim }
   {
    \msg_warning:nn { zafka } { rerun }
   }
  \iow_shipout:cx { @auxout }
   {% save the maximum width collected so far
    \global\transcriptlen=\dim_eval:n { \g_zafka_width_dim } \relax
   }
 }

\msg_new:nnn { zafka } { rerun }
 {
  Label~width~in~transcript~has~changed.~Rerun~LaTeX.
 }

\ExplSyntaxOff

\setspeaker{Bob}
\setspeaker{Billy}[Billy Bob]
\setspeaker{xxx}[Xavier Xanadu III]

\begin{document}
\begin{description}

  \Bob I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \Billy I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \Bob I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \xxx Perhaps something that aligns the names to the text, and uses a
  columnar layout for the people vs what they said.

  \Bob I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \Billy I'm looking for a package to help with the layout of an
  interview transcript are there any good packages to do such a
  task. If possible could you provide a minimum working example?

  \xxx Perhaps something that aligns the names to the text, and uses a
  columnar layout for the people vs what they said.

\end{description}
\end{document}

在此处输入图片描述

相关内容