提问于:
浏览数:
1965
https://zhuanlan.zhihu.com/p/51494365
这个例子运行不了
缺少\settototalheight的定义?
***multilingualTitles-examples.tex***
```tex
\documentclass{ctexart}
% see https://zhuanlan.zhihu.com/p/41721110
\input multilingualTitles-code
\newtheorem{example}{Example}[section]
% definitions
\begin{document}
\begin{example}{Empty input}
\multilingualTitles
{}
{}
\end{example}
\begin{example}{Short titles}
\multilingualTitles
{啦啦啦}
{lalala}
\end{example}
\begin{example}{Long titles, auto line break with shorter line width}
\multilingualTitles
{很长长长长长长长长长长长长长长长长长的标题}
{Very Looong Looong Looong Looong Looong Looong Title}
\end{example}
\begin{example}{Short title along with long title 1}
\multilingualTitles
{很一二三四五一二三四五一二三四五的标题}
{Very Looong Looong Looong Looong Looong Looong Title}
\end{example}
\begin{example}{Short title along with long title 2}
\multilingualTitles
{很一二三四五一二三四五一二三四五一的标题}
{Very Looong Looong Looong Looong Title}
\end{example}
\begin{example}{Long Titles, manual linebreak}
\multilingualTitles
{很长长长长长长\\长长长长长长长长长长的标题}
{Very Looong Looong Looong\\ Looong Looong Title}
\end{example}
\begin{example}{Titles with math formulae}
\multilingualTitles
{啦啦啦 $x^n + y^n = z^n$ 啦啦啦}
{lalala $\sin x$ lalala}
\end{example}
\end{document}
```
***multilingualTitles-code.tex***
```tex
\makeatletter
\renewcommand{\arraystretch}{1.2}
% inner temp length
\newlength\@tempTitleHt
\newlength\@tempTitleMaxWd
% length used as parameters, "Wd" is a shorthand for "Width"
\newlength\titleSingleLineMaxWd
\newlength\titleMultiLineMaxWd
\newlength\titleLRExtraWd
\newlength\titleSepWd
\setlength\titleSingleLineMaxWd{0.6\textwidth}
\setlength\titleMultiLineMaxWd{0.5\textwidth}
\setlength\titleLRExtraWd{.5em}
\setlength\titleSepWd{.5em}
% main macro, using a simple table
\newcommand{\multilingualTitles}[2]{%
\par
{\noindent\centering
\begin{tabular}{r @{\hspace{\titleSepWd}} p{\titleSingleLineMaxWd}}
中文标题 & \titleBox{#1} \\
& \\[-10pt]
Title in English & \titleBox{#2}
\end{tabular}\par}%
}
% draw a fixed-width underline
\newcommand{\titleUnderline}{%
\rule[-.3em]{\titleSingleLineMaxWd + 2\titleLRExtraWd}{.5pt}}
% title content typesetter
\newcommand\titleBox[1]{%
\setlength\@tempTitleMaxWd\titleSingleLineMaxWd
% Measure the total height of #1 subject to \titleSingleLineMaxWd.
% Here \@titleBox is necessary when #1 contains "\\".
\settototalheight\@tempTitleHt{\@titleBox{#1}}%
\ifdim\@tempTitleHt=0pt%
% case 1: #1 causes empty output
\titleUnderline
\else
% Change to LR mode, for inserting a zero-width box right after.
\leavevmode
% note: Use \normalbaseineskip instead of \baselineskip,
% since the latter is set to 0pt inside tabular env.
\ifdim\@tempTitleHt>\normalbaselineskip
% case 2: #1 is fit for multilines, or contains `\\`, hence
% \titleMultiLineMaxWd is used instead, and the total
% height of #1 subject to new max width is re-measured.
\setlength\@tempTitleMaxWd\titleMultiLineMaxWd
\settototalheight\@tempTitleHt{\@titleBox{#1}}%
% \rlap{\smash{...}} typesets content of its argument normally
% but sets it into a zero sized box. Here \@titleBox set
% (possiblly) multi-line text into a single box, since \smash
% only takes in argument spreading one line.
%
% Every line of title content needs an underline, hense we do
% a loop to typeset one underline for every line the title
% content actually spreads.
\rlap{\smash{\@titleBox{%
\@whiledim\@tempTitleHt>0pt%
\leavevmode
%
\do{%
\rlap{\titleUnderline}\\%
\addtolength\@tempTitleHt{-\normalbaselineskip}%
}%
}}}%
% Insert h-offset to manually center the title content.
\hspace{\dimexpr\titleLRExtraWd + .5\titleSingleLineMaxWd - .5\titleMultiLineMaxWd\relax}%
\@titleBox{\centering #1}%
\else
% case 3: #1 is fit for one line
\rlap{\titleUnderline}%
\hspace{\titleLRExtraWd}%
\@titleBox{\centering #1}%
\fi
\fi
}
% common operations on every title content
\newcommand{\@titleBox}[1]{%
\parbox[t]{\@tempTitleMaxWd}{#1}}
\makeatother
```