Discussion:
natbib: optional arguments with \citenum?
2020-05-03 09:38:33 UTC
Background:

I don't want to have to change the .tex file depending on whether
numerical or author-year citations are used. For now, I'm concerned
only with something like

\citep[\eg{}][and references therein]{key}

(\eg is my own macro, mainly to be able to switch between italic and
roman depending on journal style; out of habit I end at least my own
macros with {} even if it is not necessary since it makes them easier to
search and replace if necessary, but actually I try to avoid having to
do that, which won't work anyway if they are more than just
abbreviations to save typing, contain arguments, perhaps optional, and
so on.)

I defined a command

\newcommand*{\citeg}[2]{\citep[\eg{}][#2]{#1}{}}

which works fine (whether or not #2 is present) with author-year
citations.

Changing it to

\renewcommand*{\citeg}[2]{(\eg{}~refs.~\citenum[#2]{#1})}

doesn't work, presumably because \citenum has no optional arguments.

Bug or feature?

I changed the redefinition to

\renewcommand*{\citeg}[2]{(\eg{}~refs.~\citenum{#1}{#2})}

which works, though the second argument has to be present even if empty
and I have to do punctuation and spacing myself (but that is OK), e.g.:

\citeg{BFamaeySMcGaugh2012a,RSanders16a}{, and references therein}

(I guess I could make my own optional argument with ifnextchar and so
on, but that is overkill for now, especially since with \citep I have to
give an empty second optional argument if I want just the first optional
argument.)

It would be nice if natbib did what I want, i.e. with citep if there are
any optional arguments, instead of printing just numbers print

(<first optional argument> number(s) <second optional argument>)

Has someone already solved this problem?

Actually, it is worth the trouble just for author-year citations, so I
can write

\citeg{BFamaeySMcGaugh2012a,RSanders16a}{}

\citep[\eg{}][]{BFamaeySMcGaugh2012a,RSanders16a}
2020-05-03 09:40:36 UTC
Testing to see why my newsserver coded the subject in UTF.
Post by Phillip Helbig (undress to reply)
I don't want to have to change the .tex file depending on whether
numerical or author-year citations are used. For now, I'm concerned
only with something like
\citep[\eg{}][and references therein]{key}
(\eg is my own macro, mainly to be able to switch between italic and
roman depending on journal style; out of habit I end at least my own
macros with {} even if it is not necessary since it makes them easier to
search and replace if necessary, but actually I try to avoid having to
do that, which won't work anyway if they are more than just
abbreviations to save typing, contain arguments, perhaps optional, and
so on.)
I defined a command
\newcommand*{\citeg}[2]{\citep[\eg{}][#2]{#1}{}}
which works fine (whether or not #2 is present) with author-year
citations.
Changing it to
\renewcommand*{\citeg}[2]{(\eg{}~refs.~\citenum[#2]{#1})}
doesn't work, presumably because \citenum has no optional arguments.
Bug or feature?
I changed the redefinition to
\renewcommand*{\citeg}[2]{(\eg{}~refs.~\citenum{#1}{#2})}
which works, though the second argument has to be present even if empty
\citeg{BFamaeySMcGaugh2012a,RSanders16a}{, and references therein}
(I guess I could make my own optional argument with ifnextchar and so
on, but that is overkill for now, especially since with \citep I have to
give an empty second optional argument if I want just the first optional
argument.)
It would be nice if natbib did what I want, i.e. with citep if there are
any optional arguments, instead of printing just numbers print
(<first optional argument> number(s) <second optional argument>)
Has someone already solved this problem?
Actually, it is worth the trouble just for author-year citations, so I
can write
\citeg{BFamaeySMcGaugh2012a,RSanders16a}{}
\citep[\eg{}][]{BFamaeySMcGaugh2012a,RSanders16a}
Holger Schieferdecker
2020-05-04 07:18:19 UTC
Post by Phillip Helbig (undress to reply)
I don't want to have to change the .tex file depending on whether
numerical or author-year citations are used. For now, I'm concerned
only with something like
\citep[\eg{}][and references therein]{key}
(\eg is my own macro, mainly to be able to switch between italic and
roman depending on journal style; out of habit I end at least my own
macros with {} even if it is not necessary since it makes them easier to
search and replace if necessary, but actually I try to avoid having to
do that, which won't work anyway if they are more than just
abbreviations to save typing, contain arguments, perhaps optional, and
so on.)
I defined a command
\newcommand*{\citeg}[2]{\citep[\eg{}][#2]{#1}{}}
which works fine (whether or not #2 is present) with author-year
citations.
Changing it to
\renewcommand*{\citeg}[2]{(\eg{}~refs.~\citenum[#2]{#1})}
doesn't work, presumably because \citenum has no optional arguments.
Bug or feature?
I changed the redefinition to
\renewcommand*{\citeg}[2]{(\eg{}~refs.~\citenum{#1}{#2})}
which works, though the second argument has to be present even if empty
\citeg{BFamaeySMcGaugh2012a,RSanders16a}{, and references therein}
(I guess I could make my own optional argument with ifnextchar and so
on, but that is overkill for now, especially since with \citep I have to
give an empty second optional argument if I want just the first optional
argument.)
You could use package xparse to define your commands. Then you don't
have to struggle with ifnextchar for several optional arguments. It
provides functions to check if a value for an argument was given. If you
need to test for an empty value you could write an own test command:

https://tex.stackexchange.com/a/33756

Holger