About
Shop
LaTeX
Software
Books
Gallery
News
Contact
Blog
Settings
Account

Exercise 5: Oxford Comma (Solution)

This is a solution to Exercise 5.
\documentclass[captions=tableabove]{scrartcl}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{etoolbox}

\newcommand*{\surnamesep}{}%
\newcommand*{\lastsurname}{}%
\newcommand*{\prelastsurname}{}%
\newcommand*{\prelastsurnamesep}{}

\newcommand{\displaynames}[1]{%
  \renewcommand*{\surnamesep}{}%
  \renewcommand*{\lastsurname}{}%
  \renewcommand*{\prelastsurname}{}%
  \renewcommand*{\prelastsurnamesep}{}%
  \renewcommand*{\do}[1]{%
    \surnamesep
    \lastsurname
    \renewcommand{\lastsurname}{%
      \renewcommand{\surnamesep}{, 
        \renewcommand*{\prelastsurnamesep}{,}}%
      \renewcommand{\prelastsurname}{\prelastsurnamesep\ and }%
      ##1%
    }%
  }%
  \docsvlist{#1}%
  \prelastsurname \lastsurname
}

\begin{document}

\displaynames{Parrot,Canary,Zebra,Arara,Duck}

\displaynames{Parrot,Canary,Zebra,Arara}

\displaynames{Parrot,Canary,Zebra}

\displaynames{Parrot,Canary}

\displaynames{Parrot}

\end{document}

Download oxfordcomma.tex or oxfordcomma.pdf.

© 2015 Dickimaw Books. "Dickimaw", "Dickimaw Books" and the Dickimaw parrot logo are trademarks. The Dickimaw parrot was painted by Magdalene Pritchett.

Terms of Use Privacy Policy Cookies Site Map FAQs