如何在 LaTeX 中实现文本居中

如何在 LaTeX 中实现文本居中

如何制作如下的直行文字?

尤其是,单词上方的数字相对于该单词居中。我尝试使用 hbox 和 centering 命令,但没有用。

\def\wrd#1#2{$\vcenter{\hbox{\valign{&\hbox{\centering##}&\hbox{\strut##}\cr#1&#2\cr}}}$}

答案1

您需要\vbox\halign,而不是\valign

\documentclass{article}

\newcommand\wrd[2]{%
  \leavevmode
  \vbox{\offinterlineskip
    \halign{%
      \hfil##\hfil\cr
      {\footnotesize\sffamily\vphantom{p}#1}\cr
      \noalign{\vskip\lineskip}%
      \vphantom{A}#2\cr
    }%
  }%
}

\begin{document}

\linespread{1.8}\selectfont


\wrd{9176}{Longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}

\end{document}

在此处输入图片描述

答案2

expex该包专为创建行间语言学示例而设计,并且可以轻松地适应这种类型的文本。

为了将数字置于单词(或单词组)上方的中央,请使用expex选项glwordalign=center

% !TEX TS-program = XeLaTeX

\documentclass{article}

\usepackage{fontspec}
\setmainfont{Palatino Linotype}
\setsansfont{Arial}
\newfontfamily\versefont{Tahoma}

\usepackage{expex}
\lingset{everygla=\footnotesize\sffamily, % formatting the numbers in the gla line
    glwordalign=center, % center alignment within glwords
    aboveglbskip=-0.4ex, % narrows the vertical gap between the gla and glb lines
    glhangstyle=none, % no hanging indent
    glspace=!0pt plus .2em, % widens the allowable space between glwords to avoid overfull lines
    glrightskip=0pt plus .5\hsize} % widens the allowable space between the right margin and the end of the last glword on a line to avoid overfull lines

\newcommand{\vs}[1]{{\bfseries\versefont #1}}

\begin{document}
\exdisplay
\begingl
\gla 976 {} 1078 2424 5547 5207 1138 5207 11 {} 11 1080 o2464 1161 2464 1080 o2384 1161 2384 1080 o2455 2532 o80 846 {} 1161 2455 1080 o5329 2532 o2196 1537 o2283 1161 5329 1080 o2074 1161 2074 1080 o689 {} 1161//
\glb \underline{Zwój księgi} (o) narodzinach Jezusa Chrystusa, syna Dawida, syna Abrahama. \vs{2} Abraham zrodził Izaaka, a Izaak zrodził Jakuba, a Jakub zrodził Judę, i braci jego, \vs{3} a Juda zrodził Faresa i Zarę z Tamary, a Fares zrodził Esroma, a Esrom zrodził Arama, \vs{4} a//
\endgl
\xe
\end{document}

在此处输入图片描述

上面的示例需要使用 XeLaTeX 或 LuaLaTeX 进行编译,但这只是因为我选择字体的方式fontspec以及使用 Unicode 输入波兰语的方式。如果您选择字体并以不同的方式输入波兰语,则可以使用 pdfLaTeX。

Alan Munn 的回答做直行文本如果您有兴趣从外部索引文件导入数字。

我无法让该lettrine包(用于首字下沉章节号)很好地与expex换行符配合;如果这对您很重要,您可以在单独的问题中询问这个问题。

答案3

外行人的方法 tabular

\documentclass{article}

\newcommand\wrd[2]{%
  \renewcommand\arraystretch{0.4}
  \begin{tabular}[t]{@{}c@{}}
   \footnotesize\textsf{#1}\\%
      #2
   \end{tabular}
}

\begin{document}

\linespread{1.8}\selectfont


\wrd{9176}{Longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}
\wrd{9176}{longword} \wrd{12345}{a} \wrd{o12}{end}

\end{document}

在此处输入图片描述

答案4

使用堆栈。哎呀。我看到 azetina 抢先了。我的 MWE 更详细一些,但还是投他一票吧。

\documentclass[12pt]{article}
\usepackage{stackengine}
\usepackage{palatino,setspace}
\newcommand\wrd[2]{\def\stacktype{L}\stackon[10pt]{#1}{\scriptsize\sffamily#2}}
\newcommand\verseno[1]{\bgroup\bfseries\sffamily#1\egroup}
\begin{document}
\doublespacing
\verseno{1} \wrd{\underline{Zwoj ksiegi}}{976} (o) \wrd{narodzinach}{1078} \wrd{Jesuza}{2424}
\wrd{Chrystusa}{5547}, \wrd{syna}{5207} \wrd{Dawida}{1138}, \wrd{syna}{5207}
\wrd{Abrahama}{11}. \verseno{2} \wrd{Abraham}{11} \wrd{zrodzil}{1080}
\wrd{Izaaka}{o2462}, \wrd{a}{1161} \wrd{Izaak}{2464} \wrd{zrodzil}{1080}
\end{document}

在此处输入图片描述

相关内容