# Author formatting of multiline quotes in margin

I use my own command to typeset quotes in the margin of my document using pdflatex and memoir, see the MWE below. Each quote consists of the actual quote itself and its author. The problem I have is that the author is always typeset on a new line, even if there is enough space to have it appear, with appropriate spacing, after the end of the quote on the same line.

In the MWE below, it seems to me that the author of the second quote should not be typeset on a new line, but on the same line. Is there a way in which the following could be achieved automatically:

1. If the quote fits onto a single line, the author follows on a new line. (First quote in MWE.)
2. If the quote does not fit onto a single line, but there is enough space after the end of the quote (not clear what "enough" is, I admit), then the author should be listed on the same line at the end of the quote. (This is what I would like to get in the second quote in MWE, but don't at the moment.)
3. If the quote does not fit onto a single line, and there is not enough space after the end of the quote, then the author is listed on a new line. (Third quote in MWE.)

MWE:

\documentclass[a4paper,10pt,draft]{memoir}
\usepackage{lipsum}
%%% formatting
\setstocksize{29.7cm}{21.0cm} % A4 stock
\settrimmedsize{27.94cm}{21.0cm}{*} % 27.94cm = 11in
\setlength{\trimtop}{0.0cm}
\setlength{\trimedge}{\stockwidth}
\settypeblocksize{23.94cm}{12.3cm}{*}
\setlrmargins{2.0cm}{*}{*}
\setulmargins{2.0cm}{*}{*}
\setmarginnotes{\onelineskip}{5.28cm}{\onelineskip}
%%% side notes
\marginparmargin{outer}
\newcommand{\mysidenote}[1]{\strictpagecheck\marginpar{\footnotesize #1}%
}
%%% side quotes - building on mysidenote
\newcommand{\mysidequote}[2]{\strictpagecheck
\marginpar{%
\raggedright\footnotesize\textsl{#1}%
\newline\hspace*{\fill} ---{#2}%
}
}
\checkandfixthelayout
\begin{document}
\lipsum[1]
\mysidequote{I don't like being quoted in the margins.}{A. N. Other}
\lipsum[2]
\mysidequote{Some quotes are too long to fit onto a single line.}{Y. A. N. Other}
\lipsum[3]
\mysidequote{Some quotes are too long to fit onto a single line and need two
full lines.}{Y. A. N. Other}
\end{document}

• Then what is that explicit \newline doing in \mysidequote? Also have a look at memoirs \sourceatright command – daleif Jan 29 '14 at 11:51
• I know, but that is the only way in which I could get quote 1 to work as I wanted... – user1362373 Jan 29 '14 at 12:07

This works just fine

\strictpagecheck
\newcommand{\mysidequote}[2]{
\marginpar{%
\raggedright\footnotesize\textsl{#1}%
%\newline\hspace*{\fill} ---{#2}%
\sourceatright{---#2}
}
}


EDIT: This seems to work, it is a copy of some macros I use internally, actually it is modified version of macros found in dlfltxbmisc on CTAN. By ideas from Morten Høgholm and Dan Luecking

\makeatletter
\newdimen\lastlinedim
\def\getlastlinesize{%
\begingroup\frozen@everydisplay{}%
\abovedisplayskip=\z@skip \abovedisplayshortskip=\z@skip \belowdisplayskip=\z@skip \belowdisplayshortskip=\z@skip \global\lastlinedim=\dimexpr\predisplaysize-2em\relax \halign{##\cr}%
\mbox{}%
\endgroup%
}
\getlastlinesize{}%
\vskip-2\baselineskip%
\begingroup%
\raggedleft%
\fussy%
\fboxsep=0pt%
\fboxrule=0pt%
\fbox{\hbox to \lastlinedim{\hfill \strut   \hfill}}%
\ifdim \lastlinedim > 0.6\linewidth\relax% we break
\nopagebreak\vskip0pt%
\else%
% special thanks to Dan Luecking for this, since my original
% \hspace{\fill} failed miserably
\hskip1em plus 5em minus .7em
\fi%
{---#1\strut} %
\par%
\endgroup%
}
\makeatother
\newcommand{\mysidequote}[2]{
\marginpar{%
\raggedright\footnotesize\textsl{#1}%