关于题词长度的一些逻辑

关于题词长度的一些逻辑

一开始我想要一种方法来指定每个规则的长度\epigraph调用的规则长度。但是在给出建议之后这里,我最终得到了一些代码来计算规则的长度,如下所示:它是文本或作者的长度,以较长者为准。除非文本(或作者...)的长度大于页面宽度的 80%,在这种情况下,规则(和文本/作者)的长度设置为页面长度的 80%。但由于说话很便宜,这里是代码:

\newcommand{\mytextformat}{\itshape\epigraphsize}
\newcommand{\mysourceformat}{\scshape}
\newenvironment{mytext}{\mytextformat}{}
\newenvironment{mysource}{\mysourceformat\hfill}{}
\renewcommand{\textflush}{mytext} 
\renewcommand{\sourceflush}{mysource}
\newlength{\epitextlen}
\newlength{\episourcelen}
\newlength{\epilength}

\let\originalepigraph\epigraph

\renewcommand\epigraph[2]%
{%  
  \setlength{\epitextlen}{\widthof{\mytextformat#1}}%
  \setlength{\episourcelen}{\widthof{\mysourceformat#2}}%
  \ifdim\epitextlen>\episourcelen%
    \setlength{\epilength}{\epitextlen}%
  \else%
    \setlength{\epilength}{\episourcelen}%
  \fi%
  \ifdim\epilength>0.8\textwidth%
    \setlength{\epilength}{0.8\textwidth}%
  \fi%
  \setlength{\epigraphwidth}{\epilength}%
  \originalepigraph{#1}{#2}%
}

它运行良好,但由于我是 TeX hackery 的新手,我想问是否可以/如何改进它?特别是,当作者的名字比文本大时,一些内容会留在它前面。这不是太严重,但如果有人能告诉我原因,我将不胜感激。提前致谢。

编辑:这是 MWE。@Steven B. Segletes:\mysourceformat按照您的建议更改该行会导致作者行换行成两行。

\documentclass[oneside]{scrbook}

\usepackage{epigraph}
\usepackage{calc}

\newcommand{\mytextformat}{\itshape\epigraphsize}
\newcommand{\mysourceformat}{\scshape}
\newenvironment{mytext}{\mytextformat}{}
\newenvironment{mysource}{\mysourceformat\hfill}{}
\renewcommand{\textflush}{mytext} 
\renewcommand{\sourceflush}{mysource}
\newlength{\epitextlen}
\newlength{\episourcelen}
\newlength{\epilength}

\let\originalepigraph\epigraph 

\renewcommand\epigraph[2]%
{%
    \setlength{\epitextlen}{\widthof{\mytextformat#1}}%
    \setlength{\episourcelen}{\widthof{\mysourceformat#2}}%
    \ifdim\epitextlen>\episourcelen%
        \setlength{\epilength}{\epitextlen}%
    \else%
        \setlength{\epilength}{\episourcelen}%
    \fi%
    \ifdim\epilength>0.8\textwidth%
        \setlength{\epilength}{0.8\textwidth}%
    \fi%
    \setlength{\epigraphwidth}{\epilength}%
    \originalepigraph{#1}{#2}%
}

\begin{document}

\chapter{Introduction}
    \epigraph{IRAV IVQV IVPV}{Gaius Julius C\ae sar} Lorem ipsum.

\end{document}

答案1

为了回答 OP 的直接问题,我提供了此修订。 \epigraphsize必须进入\mysourceformat;我还更新了\renewcommand{\textflush}{flushleft},和\renewcommand{\sourceflush}{flushright}。最后,需要通过以下方式将所需的格式应用于题词\originalepigraph{\mytextformat#1}{\mysourceformat#2}

\documentclass[oneside]{scrbook}

\usepackage{epigraph}
\usepackage{calc}

\newcommand{\mytextformat}{\epigraphsize\itshape}
\newcommand{\mysourceformat}{\epigraphsize\scshape}
\newenvironment{mytext}{\mytextformat}{}
\newenvironment{mysource}{\mysourceformat\hfill}{}
\renewcommand{\textflush}{mytext} 
\renewcommand{\sourceflush}{mysource}
\newlength{\epitextlen}
\newlength{\episourcelen}
\newlength{\epilength}

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


\let\originalepigraph\epigraph 

\renewcommand\epigraph[2]%
{%
    \setlength{\epitextlen}{\widthof{\mytextformat#1}}%
    \setlength{\episourcelen}{\widthof{\mysourceformat#2}}%
    \ifdim\epitextlen>\episourcelen%
        \setlength{\epilength}{\epitextlen}%
    \else%
        \setlength{\epilength}{\episourcelen}%
    \fi%
    \ifdim\epilength>0.8\textwidth%
        \setlength{\epilength}{0.8\textwidth}%
    \fi%
    \setlength{\epigraphwidth}{\epilength}%
    \originalepigraph{\mytextformat#1}{\mysourceformat#2}%
}

\begin{document}
\chapter{Introduction}
    \epigraph{IRAV IVQV IVPV}{Gaius Julius C\ae sar} Lorem ipsum.

\end{document}

但这里有一个略有不同的方法。它将文本和作者堆叠在一起,并且堆栈宽度将是两者中较大的一个。它使用该堆栈宽度与进行比较.8\linewidth

行宽的关键在于引用是在 中设置的\epigraphsize。因此,被测试宽度的框也应该在 中设置\epigraphsize。然后,只有在换行时,行才会超出引用或作者,因为换行的文本是不整齐的。(请注意, \textflushOP 引用的答案中的宏会导致文本向右对齐,而不是向左对齐;可以将其注释掉)。

我已编辑答案以允许文本或作者的换行,这将强制线宽达到换行长度。

\documentclass{book}
\usepackage{epigraph}
\usepackage[usestackEOL]{stackengine}
\renewcommand{\epigraphsize}{\small}
\newcommand{\mytextformat}{\epigraphsize\itshape}
\newcommand{\mysourceformat}{\epigraphsize\scshape}


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

\let\originalepigraph\epigraph 
\renewcommand\epigraph[2]{%
  \setbox0=\hbox{\stackon{\textit{\mytextformat\Longstack{#1}}}%
    {\mysourceformat\scshape\Longstack{#2}}}%
  \ifdim\wd0>.8\linewidth\wd0=.8\linewidth\fi%
  \setlength{\epigraphwidth}{\wd0}%
  \originalepigraph{\textit{#1}}{\textsc{#2}}%
}

\begin{document}

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

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

Left to autowrap:

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

\epigraph{All models are without a doubt, very, very, wrong, but some might be marginally useful.}{George E. P. Box}

Forcing a linebreak:

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

\epigraph{All models are without a doubt, very, very, wrong, but some might\\ be marginally useful.}{George E. P. Box}

Longer author than quote:

\epigraph{Nuts!}{General Anthony Clement "Nuts" McAuliffe}

\epigraph{Nuts!}{General Anthony Clement "Nuts" McAuliffe (July 2, 1898 -- August 11, 1975), United States Army, acting division commander, 101st Airborne Division,  defending Bastogne, Belgium, during World War II's Battle of the Bulge}

\end{document}

在此处输入图片描述

相关内容