![将角色名称对齐到内部](https://linux22.com/image/451073/%E5%B0%86%E8%A7%92%E8%89%B2%E5%90%8D%E7%A7%B0%E5%AF%B9%E9%BD%90%E5%88%B0%E5%86%85%E9%83%A8.png)
我建了一个剧本编写源文件。我想知道是否可以将里面的角色名字对齐,如图所示。
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{titlesec}
% fonts
\usepackage{nimbusmononarrow}
\renewcommand*\familydefault{\ttdefault}
% def characters
\newcommand\MAN{\par\vspace{5mm}\noindent\makebox[35mm][l]{MAN}\hangindent=35mm }
\newcommand\WOMAN{\par\vspace{5mm}\noindent\makebox[35mm][l]{WOMAN}\hangindent=35mm }
\begin{document}
\MAN
This is my first joke!
\WOMAN
This is my first joke!
\end{document}
答案1
你可以使用类似这样的命令。如果你不能使用某个角色名称作为命令,请使用可选参数来\newcharacter
指定命令名称。
示例:定义在脚本中打印“MAN”作为角色名称的\newcharacter[ManCmd]{MAN}
命令。\ManCmd
\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum} % for the dummy text
% Fonts
\usepackage{nimbusmononarrow}
\renewcommand*\familydefault{\ttdefault}
\newlength{\textOffset}
\setlength{\textOffset}{35mm}
\newlength{\maxCharnameLen}
\setlength{\maxCharnameLen}{15mm}
\ExplSyntaxOn
\NewDocumentCommand \newcharacter { O{#2} m }
{
\cs_new_protected:cpn {#1}
{
\par \vspace { 5mm } \noindent
\makebox [\textOffset] [l] { \makebox [\maxCharnameLen] [r] {#2} }
\dim_set_eq:NN \hangindent \textOffset
}
}
\ExplSyntaxOff
\newcharacter{MAN}
\newcharacter{WOMAN}
\begin{document}
\MAN
This is my first joke!
\WOMAN
This is my first joke! \lipsum*[1][1-3]
\end{document}
答案2
您可以定义一个抽象。
这是一个还测量名称的版本,因此它可以将空间设置为严格必要的宽度。或者,您可以决定名称是否应保留在边距中。
\documentclass[a4paper,12pt]{article}
% fonts
\usepackage{nimbusmononarrow}
\renewcommand*\familydefault{\ttdefault}
% def characters
\newlength{\characterswidth}
\newlength{\characterssep}
\NewDocumentCommand{\definecharacter}{smm}{%
% #1 = possible * (no adjustment to the global width)
% #2 = command
% #3 = name
\NewDocumentCommand{#2}{}{%
\par\addvspace{5mm}
\hangindent=\dimexpr\characterswidth+\characterssep\relax
\noindent
\makebox[\characterswidth][r]{#3}\hspace{\characterssep}%
}%
\IfBooleanT{#1}{%
\AtBeginDocument{%
% measure the name
\settowidth{\dimen0}{#3}%
\ifdim\characterswidth<\dimen0
\setlength{\characterswidth}{\dimen0}%
\fi
}%
}%
}
\AtBeginDocument{\setlength{\characterssep}{2em}}
\definecharacter{\MAN}{MAN}
\definecharacter{\WOMAN}{WOMAN}
\definecharacter{\TOOLONG}{STICKSINMARGIN}
\begin{document}
\raggedright
\MAN
This is my first joke!
\WOMAN
This is my first joke!
\MAN
This is my first joke! This is my first joke! This is my first joke!
This is my first joke! This is my first joke! This is my first joke!
This is my first joke! This is my first joke! This is my first joke!
This is my first joke! This is my first joke! This is my first joke!
\TOOLONG
Not a joke at all.
\end{document}
在文档开始发布时需要进行测量\normalfont
,因此将使用所选的字体进行测量。