Some logic for the epigraph length

In the beginning I wanted a way to specify the rule length for each \epigraph call. But after the suggestion given here, I ended up with some code that calculates the length of the rule like this: it is the length of either the text or the author, whichever is longer. Unless the length of the text (or of the author...) is larger the 80% of the width of the page, in which case the length of the rule (and the text/author) is set to 80% of that of the page. But as talking is cheap, here's the code:

\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}%
}


It works well, but as I am new to TeX hackery, I wanted to ask if/how it could be improved? In particular, when the author's name is larger than the text, some is left before it. It's not too serious, but if anyone can tell me why, I'd appreciate it. Thanks in advance.

EDIT: Here's the MWE. @Steven B. Segletes: changing the \mysourceformat line as you suggest causes the author line to be wrapped into two lines.

\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}



\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}


But here is a slightly different approach. It stacks the text and author, and the stack width will be the larger of the two. It uses that stack width for the comparison to .8\linewidth.

The key to the line width is that the quote is set in \epigraphsize. Thus, the box being tested for width should likewise be set in \epigraphsize. Then, the only places where the line extends past the quote or the author is when wrapping occurs, since the wrapped text is set ragged. (Note that the \textflush macro from the OP's referenced answer causes the text to be made flush right, rather than left; it can be commented out).

I have EDITED the answer to allow linebreaks in the text or author, which will force the linewidth to the break length.

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


\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}


• The suggestion to change the \mysourceformat line does not work. If the author's length is greater than the text's, the former will get wrapped. Thank you for the alternative, I'll try it tomorrow! – wmnorth Feb 2 '15 at 21:08
• @wmnorth I'm not sure I understand the problem with the author wrapping, if it is long. But since you didn't provide a minimum working example to compile, I couldn't test my assertion on your code. – Steven B. Segletes Feb 2 '15 at 21:18
• I have now added the MWE, and hopefully elaborated on what my issue is. – wmnorth Feb 2 '15 at 22:56
• @wmnorth I have provided a version of your code that I think fixes the issue, as well as my original code that works. – Steven B. Segletes Feb 2 '15 at 23:41
• I still don't quite understand how it works, but it does work, perfectly. Thanks! – wmnorth Feb 3 '15 at 23:35