碑文宽度

碑文宽度

如何设置题词的宽度以使文字自动适应题词内部?

我希望文本和源之间的线与上面的文本有相同的宽度。每个题词都是这样!

我不喜欢的例子:

  1. 线太长

    \setlength{\epigraphwidth}{0.8\textwidth}
    \epigraph{All models are wrong, but some are useful.}{George E. P. Box}
    

    线太长

  2. 行太短,而且会添加一行我不喜欢的新行

    \epigraph{All models are wrong, but some are useful.}{George E. P. Box}
    

    在此处输入图片描述

更新: 最小源

\documentclass{book}
\usepackage[italian]{babel}

\usepackage{epigraph}
\renewcommand{\epigraphsize}{\small}

%\setlength{\epigraphwidth}{0.8\textwidth}

\renewcommand{\textflush}{flushright} \renewcommand{\sourceflush}{flushright}

\let\originalepigraph\epigraph 
\renewcommand\epigraph[2]{\originalepigraph{\textit{#1}}{\textsc{#2}}}

\begin{document}

\epigraph{All models are wrong, but some are useful.}{George E. P. Box}

\end{document}

答案1

对宏进行大量修改\epigraph似乎可以达到您的要求。借助该varwidth包,我们能够计算文本的实际宽度(感谢 David Carlisle 的建议)。

\documentclass{book}
\usepackage[italian]{babel}

\usepackage{epigraph,varwidth}

\renewcommand{\epigraphsize}{\small}
\setlength{\epigraphwidth}{0.6\textwidth}
\renewcommand{\textflush}{flushright}
\renewcommand{\sourceflush}{flushright}
% A useful addition
\newcommand{\epitextfont}{\itshape}
\newcommand{\episourcefont}{\scshape}

\makeatletter
\newsavebox{\epi@textbox}
\newsavebox{\epi@sourcebox}
\newlength\epi@finalwidth
\renewcommand{\epigraph}[2]{%
  \vspace{\beforeepigraphskip}
  {\epigraphsize\begin{\epigraphflush}
   \epi@finalwidth=\z@
   \sbox\epi@textbox{%
     \varwidth{\epigraphwidth}
     \begin{\textflush}\epitextfont#1\end{\textflush}
     \endvarwidth
   }%
   \epi@finalwidth=\wd\epi@textbox
   \sbox\epi@sourcebox{%
     \varwidth{\epigraphwidth}
     \begin{\sourceflush}\episourcefont#2\end{\sourceflush}%
     \endvarwidth
   }%
   \ifdim\wd\epi@sourcebox>\epi@finalwidth 
     \epi@finalwidth=\wd\epi@sourcebox
   \fi
   \leavevmode\vbox{
     \hb@xt@\epi@finalwidth{\hfil\box\epi@textbox}
     \vskip1.75ex
     \hrule height \epigraphrule
     \vskip.75ex
     \hb@xt@\epi@finalwidth{\hfil\box\epi@sourcebox}
   }%
   \end{\epigraphflush}
   \vspace{\afterepigraphskip}}}
\makeatother

\begin{document}

\epigraph{All models are wrong, but some are useful.}{George E. P. Box}
\epigraph{All models are wrong, but some are usefull.}{George E. P. Box}
\epigraph{All models are wrong, but some are usefulll.}{George E. P. Box}
\epigraph{All models are wrong, but some are usefullll.}{George E. P. Box}
\epigraph{All models are wrong, but some are more useful.}{George E. P. Box}
\epigraph{All models are wrong, but some are more and more useful.}{George E. P. Box}

\end{document}

在此处输入图片描述

答案2

如果您希望每个题词的宽度等于其行的宽度,那么您只需测量第一个参数的宽度并在重新定义的epigraph命令中设置宽度即可。我已经加载了calc包以轻松完成此操作。由于参数的测量需要考虑您为题词设置的格式,因此最好使用正确的epigraph方法来执行此操作。因此,我创建了一个格式化命令,然后在传递给相关命令的自定义环境中使用它epigraph

\documentclass{book}
\usepackage[italian]{babel}

\usepackage{epigraph}
\usepackage{calc}

\newcommand{\mytextformat}{\itshape\epigraphsize}
\newenvironment{mytext}{\mytextformat}{}
\newenvironment{mysource}{\scshape\hfill}{}
\renewcommand{\textflush}{mytext} 
\renewcommand{\sourceflush}{mysource}

\let\originalepigraph\epigraph 
\renewcommand\epigraph[2]%
   {\setlength{\epigraphwidth}{\widthof{\mytextformat#1}}\originalepigraph{#1}{#2}}

\begin{document}

\epigraph{All models are wrong, but some are useful.}{George E. P. Box}

\end{document}

代码输出

答案3

在序言中,使用它来定义\epiline要使用的命令,\hline但要与其他尺寸一起使用。

\newlength\epirule%
\newcommand\epiline{%
\noalign{\global\epirule\arrayrulewidth\global\arrayrulewidth 1pt}\hline%
\noalign{\global\arrayrulewidth\epirule}%
}

这里,该命令\epigraph带有两个参数(这里不需要包)。

\newcommand\epigraph[2]{%
\hfill\begin{tabular}{@{}r@{}}
{\small\textit{#1}}\\[.5em] \epiline%
{\small\textsc{#2}}
\end{tabular}
}

然后你可以使用它作为

\epigraph{All models are wrong, but some are useful}{George E.\ P.\ Box}

在此处输入图片描述

相关内容