我想要一个具有指定高度的不可见文本框环境。我有一些命令,我想用它们来打印文本。这个环境将用于水平显示文章作者的姓名。如果名字太多,框中放不下,我还希望环境在框的末尾打印“et al.” 。以下是(大致)我正在寻找的内容:
以下是 MWE:
\documentclass[fontsize=13pt,paper=a4,openany,parskip=half,DIV=calc]{scrbook}
\RequirePackage{DejaVuSans} % Sans-serif
\RequirePackage[T1]{fontenc}
\newcommand{\by}[2]
{%
{\centering
\fontsize{9pt}{11pt} \sffamily \textsf{\MakeUppercase{#1} }\\
\fontsize{7pt}{10pt} \sffamily \textsf{{#2}} \rmfamily \par}
}
%
\newcommand{\andby}
{%
{\centering
\vspace{-0.7em}
\fontsize{7pt}{10pt} \sffamily \textsf{and}\\
\rmfamily
}
}
%
\newcommand{\etal}
{%
{\centering
\vspace{-0.7em}
\fontsize{7pt}{10pt} \sffamily \textsf{et al.}\\
\rmfamily
}
}
\newenvironment{authorbox}{
% Some box with height that matches \by text and width \textwidth
}{
% End of box
}
\begin{document}
\begin{authorbox}
\by{Someone}{Tagline}
\andby
\by{Someone}{tagline}
\end{authorbox}
\end{document}
我还希望框中的文本居中。如果只有 1 个作者(1 个\by
命令),则文本应显示在中间。如果有两个作者,则单词“and”应位于中间。
注意:它需要能够“适应”长名称(约 30 个字符,包括空格)和短名称。如果给出了长名称和短名称,“和”仍应位于中间。
编辑:我希望有某种“向后兼容性”。以前该命令的使用方式如下:\by{someone}{tagline}
用于只有一位作者的文章。为了避免兼容性问题和不得不编辑旧文章,我希望该命令\by{someone}{tagline}
仍能以类似的方式工作。
答案1
你可以吸收所有作者并做一些测量。
\RequirePackage{fix-cm}
\documentclass[fontsize=13pt,paper=a4,openany,parskip=half,DIV=calc]{scrbook}
\usepackage[T1]{fontenc}
\usepackage{DejaVuSans} % Sans-serif
\usepackage{showframe}
\ExplSyntaxOn
% for the production version, comment the second definition
\cs_new_protected:Nn \__vebjorn_by_author_print:n
{
\makebox[\l__vebjorn_by_widest_dim]{#1}
}
%%% comment from here
\cs_set_protected:Nn \__vebjorn_by_author_print:n
{
\group_begin:
\dim_set:Nn \fboxsep { 0pt }
\framebox[\l__vebjorn_by_widest_dim]{#1}
\group_end:
}
%%% to here
\NewDocumentCommand{\by}{m}
{
\vebjorn_by:n { #1 }
}
\seq_new:N \l__vebjorn_by_in_seq
\seq_new:N \l__vebjorn_by_out_seq
\dim_new:N \l__vebjorn_by_widest_dim
\box_new:N \l__vebjorn_by_author_box
\int_new:N \l__vebjorn_by_max_int
\cs_new_protected:Nn \vebjorn_by:n
{
\dim_zero:N \l__vebjorn_by_widest_dim
\seq_set_split:Nnn \l__vebjorn_by_in_seq { \\ } { #1 }
\seq_set_map:NNn \l__vebjorn_by_out_seq \l__vebjorn_by_in_seq { \__vebjorn_by_author:nn ##1 }
\__vebjorn_by_measure:
}
\cs_new_protected:Nn \l__vebjorn_by_fil:
{
\int_compare:nTF { \seq_count:N \l__vebjorn_by_out_seq < 3 } { \hfil } { \hfill }
}
\cs_new_protected:Nn \__vebjorn_by_author:nn
{
\begin{tabular}[t]{@{}c@{}}
\fontsize{9}{11}\sffamily \text_uppercase:n{#1} \\[1ex]
\fontsize{7}{10}\sffamily #2
\end{tabular}
}
\cs_new_protected:Nn \__vebjorn_by_measure:
{
\seq_map_function:NN \l__vebjorn_by_out_seq \__vebjorn_by_measure_author:n
\int_set:Nn \l__vebjorn_by_max_int { \textwidth / \l__vebjorn_by_widest_dim - 1 }
% now we can print!
\begin{center}
\renewcommand{\arraystretch}{0}
\seq_indexed_map_inline:Nn \l__vebjorn_by_out_seq
{
\__vebjorn_by_author_print:n {##2}
\int_compare:nTF { ##1 < \l__vebjorn_by_max_int }
{
\int_compare:nT { ##1 < \seq_count:N \l__vebjorn_by_out_seq }
{
{\l__vebjorn_by_fil:\fontsize{7}{10}\sffamily and\l__vebjorn_by_fil:}
}
}
{
\int_compare:nT { \l__vebjorn_by_max_int < \seq_count:N \l__vebjorn_by_out_seq }
{% we've got more authors than they fit
\seq_map_break:n { \l__vebjorn_by_fil:\fontsize{7}{10}\sffamily et~al. }
}
}
}
\end{center}
}
\cs_new_protected:Nn \__vebjorn_by_measure_author:n
{
\hbox_set:Nn \l__vebjorn_by_author_box
{
#1
{\fontsize{7}{10}\sffamily\quad and \quad}
}
\dim_set:Nn \l__vebjorn_by_widest_dim
{
\dim_max:nn { \l__vebjorn_by_widest_dim } { \box_wd:N \l__vebjorn_by_author_box }
}
}
\ExplSyntaxOff
\begin{document}
\by{
{Someone}{Tagline}
}
\by{
{Someone}{Tagline} \\
{Someone Else}{tagline}
}
\by{
{Someone}{Tagline} \\
{Someone Else}{tagline} \\
{Someone 2}{Tagline}
}
\by{
{Someone}{Tagline} \\
{Someone Else}{tagline} \\
{Someone 2}{Tagline} \\
{Someone 3}{Tagline} \\
{Someone 4}{Tagline}
}
\by{
{Someone}{Tagline} \\
{Someone Else}{tagline} \\
{Someone 2}{Tagline} \\
{Someone 3}{Tagline} \\
{Someone 4}{Tagline} \\
{Someone 5}{Tagline}
}
\end{document}