5

I want to create a certain cite command that is pretty similar to the original one:

\newcommand{\mycite}[2][]{\hspace{30pt}\cite{#2}{#1}}

The problem is that when I only write \mycite{test}. Then there will be

[Test, ]

but it should be

[Test]

because I omitted the optional argument. I tried it with the package xparse. But there I got the same result with the following code

\NewDocumentCommand\mycite{O{}m}{%
  \cite[#1]{#2}%
}

What do I do wrong? Could you help me?

1
  • Please be aware that some of the \cite - related packages do redefine \cite to provide more optional arguments already. – user31729 Sep 30 '15 at 13:41
5

There's no check if the optional argument of \mycite is present or not. So \mycite{#2} will still call \cite[]{#2} with an empty optional argument for \cite, meaning to typeset , in the list.

I added the 'traditional' way to check for the optional argument and the xparse method (much easier!)

Note: Bibliography related packages might change the \cite command already. This is not covered here.

\documentclass{article}

\usepackage{etoolbox}
\usepackage{xparse}

\makeatletter

\newcommand{\@myotherciteopt}[2][]{%
  \hspace{30pt}%
  \ifblank{#1}{%
    \cite{#2}%
  }{%
    \cite[#1]{#2}%
  }%
}

\newcommand{\@myothercitenoopt}[1]{%
  \@myotherciteopt[]{#1}%
}

\newcommand{\myothercite}{%
  \@ifnextchar[{\@myotherciteopt}{\@myothercitenoopt}%
}

\makeatother

\NewDocumentCommand\mycite{om}{%
  \hspace{30pt}%
  \IfValueTF{#1}{%
    \cite[#1]{#2}%
  }{%
    \cite{#2}%
  }%
}


\begin{document}

\mycite{Lam94}

\myothercite{Lam94}

Now with options:

\mycite[Ms. Ann Elk]{Lam94}

\myothercite[Ms. Ann Elk]{Lam94}

\bibliographystyle{alpha}

\bibliography{biblio}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.