8

The expex package has a nice macro \trailingcitation which typesets a citation at the right margin of the same line if there is space, or flushed right on the line if there is no space (first two examples below, using a simplified version of \trailingcitation). Unfortunately this macro cannot handle citations longer than a single line (third example).

I tried to use Peter Wilson's \atright, but it puts as little text of the citation as possible on the first line (fourth example).

What I want instead is that if the citation is longer than a single line, it starts on a new line, and is flushed right (or, ideally, justified but with the last line aligned with the right rather than the left margin) (fifth example). How do I accomplish this?


enter image description here

\documentclass[draft]{article}

\usepackage[a4paper,margin=5cm]{geometry}
\usepackage{parskip}

\def\trailingcitation#1{% adapted from expex
   \hskip 1.5em plus 1fill
   \penalty100\null\nobreak \hskip 0pt plus 1fill
   \hbox{#1}}

\newcommand*{\atright}[1]{{%
  \unskip\nobreak\hfil\penalty50
  \hskip2em\hbox{}\nobreak\hfil#1
  \parfillskip=0pt\finalhyphendemerits=0\par}}

\begin{document}

A short quote gets a citation on the same line.
\trailingcitation{(Smith 2015)}

When the citation does not fit on the same line, a new line is used.
\trailingcitation{(Smith 2015)}

Due to \verb$\hbox$, long citations cause an overfull hbox.
\trailingcitation{(Author, \textit{Some very long title of a secondary source}, in citation by Smith 2015)}

\verb$\atright$ will make the first line as short as possible:
\atright{(Author, \textit{Some very long title of a secondary source}, in citation by Smith 2015)}

Whereas I want something like this (but only if it does not fit on the same line):\par\vspace{-1ex}
{\raggedleft (Author, \textit{Some very long title of a secondary source}, in citation by Smith 2015)\par}

\end{document}

1 Answer 1

11

enter image description here

\documentclass[draft]{article}

\usepackage[a4paper,margin=5cm]{geometry}
\usepackage{parskip}

\def\trailingcitation#1{% adapted from expex
   \unskip
   \hskip 1.5em plus 1fill
   \penalty100\null\nobreak \hskip 0pt plus 1fill
   \discretionary
   {}%
   {\vtop{%
  % shorter line (if you like)
  \leftskip 20pt
  #1\par
  % reset last line flush right
  \setbox0\lastbox
  \nointerlineskip
  \hbox to \hsize{\hfill\unhbox0\unskip\unskip}}%
    }%
   {\hbox{#1}}%
}

\begin{document}

A short quote gets a citation on the same line.
\trailingcitation{(Smith 2015)}

When the citation does not fit on the same line, a new line is used.
\trailingcitation{(Smith 2015)}

Due to \verb$\hbox$, long citations cause an overfull hbox.
\trailingcitation{(Author, \textit{Some very long title of a secondary source}, in citation by Smith 2015)}


\end{document}

In lists and ExPex examples, a limited amount of horizontal space is available. The \vtop needs to be set to \hsize=\linewidth for lists, and for ExPex examples the remaining width can be computed. The following works in both ExPex examples and lists. It also uses ExPex's mincitesep and everytrailingcitation, which were removed from the MWE in the question for simplicity:

enter image description here

\documentclass[draft]{article}

\usepackage[a4paper,margin=5cm]{geometry}
\usepackage{expex}

% Alan Munn's method to get remaining space in expex examples, https://tex.stackexchange.com/a/376534:
\usepackage{calc}
\usepackage{etoolbox}
\newlength{\remaining}
\newcommand{\remainpex}{%
   \setlength{\remaining}{\linewidth-\lingtextoffset-\linglabelwidth-
      \lingnumoffset-\linglabeloffset-\widthof{\exnoprint}}%
}
\newcommand{\remainex}{%
   \setlength{\remaining}{\linewidth-\lingnumoffset-
      \lingtextoffset-\widthof{\exnoprint}}%
}

% We also need to know whether we are in an example or not:
\newif\ifinex \inexfalse
\pretocmd{\pex}{\remainpex\inextrue}{}{}
\pretocmd{\ex}{\remainex\inextrue}{}{}
\apptocmd{\xe}{\inexfalse}{}{}

\makeatletter
\def\trailingcitation#1{% adapted from expex
   \unskip
   \hskip\ling@mincitesep plus 1fill
   \penalty100\null\nobreak \hskip 0pt plus 1fill
   \discretionary
   {}%
   {\vtop{%
  \hsize=\ifinex\remaining\else\linewidth\fi
  % shorter line (if you like)
  \leftskip 20pt
  \ling@everytrailingcitation #1\par
  % reset last line flush right
  \setbox0\lastbox
  \nointerlineskip
  \hbox to \hsize{\hfill\unhbox0\unskip\unskip}}%
    }%
   {\hbox{#1}}%
}
\makeatother

\begin{document}

\ex
  Due to \verb$\hbox$, long citations cause an overfull hbox.
  \trailingcitation{(Author, \textit{Some very long title of a secondary source}, in citation by Smith 2015)}
\xe

\pex
\a
  Due to \verb$\hbox$, long citations cause an overfull hbox.
  \trailingcitation{(Author, \textit{Some very long title of a secondary source}, in citation by Smith 2015)}
\xe

\begin{itemize}
  \item
    Due to \verb$\hbox$, long citations cause an overfull hbox.
    \trailingcitation{(Author, \textit{Some very long title of a secondary source}, in citation by Smith 2015)}
\end{itemize}

\end{document}
5
  • Thanks very much! I've only seen a small problem when this macro is used by an indented environment like quote. Wrapping the last example in your answer in quote causes the citation to overflow in the right margin by the same amount as the environment is indented. I could fix this by adding \hline=\linewidth at the start of the \vtop. Since expex does not set \linewidth in examples, I needed to use this method to calculate the remaining width in \ex and \pex contexts.
    – Keelan
    Aug 18, 2023 at 8:12
  • @Keelan oh I nearly commented that directly setting leftskip and using hsize cut across latex list constructs. If you have something working for the original expex use case you could post another answer (or add a second fuller example at the end of this to help anyone who comes here following the expex tag in future Aug 18, 2023 at 8:18
  • Great, I'll add an example with expex to your answer in a bit. I'm not entirely sure what you mean by "directly setting leftskip and using hsize cut across latex list constructs" (English is not my native language), i.e. if "cut across" means this is a good solution or not -- but if you think it's good I can also add this (or you can edit it yourself).
    – Keelan
    Aug 18, 2023 at 8:27
  • @Keelan sorry, yes it means "ignore all the latex rules and just forces a layout that works in this case, using tex primitives) I hoped it was OK as these final citations are usually just simple text (not nested lists) and I needed low level \discretionary and \lastbox constructs anyway so I couldn't use high level latex constructs. Aug 18, 2023 at 8:40
  • 1
    Thanks! I added an example at the end of your answer which works for both ExPex examples and lists, using an \if to determine in which environment we are.
    – Keelan
    Aug 18, 2023 at 8:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .