General\newcommand and optional parameters

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
kristianjohann
Posts: 5
Joined: Tue Oct 20, 2009 1:31 pm

\newcommand and optional parameters

Post by kristianjohann »

Hi all!

CONTEXT.
For different journals one have to cite in different ways (in footnotes ore Harvard-citation style). To manage this in an easy way I created a cite-command (\mycite) with two optional and one mandatory parameters. This command can easily be redefined depending on a journals format.

PROBLEM.
If an optional argument is empty, then the respective parameter is identical with \empty. But the usual citation-command \cite that is used in my definition of \mycite implements (\cite[\empty][\empty]{carnap1928LogAuf}) different from (\cite[][]{carnap1928LogAuf}), namely: ( [Car28], p.) vs. ([Car28]). How to get out of (\cite[\empty][\empty]{carnap1928LogAuf}) the result ([Car28])?

FAILED-SOLUTION.
I think that it is a problem of expansion. I tried to expand first the arguments - but I only succeeded expanding the first argument: (\expandafter\cite\expandafter[\empty][\empty]{carnap1928LogAuf}) results in ([Car28], p.)

IMPLEMENTATION.

Code: Select all

\newcommand{\mycite}[1][\empty]{%
  \def\ArgI{#1}%
  \mycitemycite%
}
\newcommand\mycitemycite[1][\empty]{%
  \def\ArgII{#1}%
  \mycitemycitemycite%
}
\newcommand\mycitemycitemycite[1]{%
  (\cite[\ArgI][\ArgII]{#1})%
}
Thanks for your attention!
Last edited by kristianjohann on Wed Jan 12, 2011 7:33 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

\newcommand and optional parameters

Post by sommerfee »

kristianjohann wrote:If an optional argument is empty, then the respective parameter is identical with \empty.
1. It's not clear to me why you use [\empty] as optional argument when defining your macros. Why don't you just use [] instead, e.g. \newcommand{\mycite}[1][]{...} ?

2. You can't use \expandafter here because you have two arguments to expand and you don't know what's inside these arguments. It could be something which doesn't like to get expanded before. So better use \edef and \noexpand instead, e.g.

Code: Select all

\newcommand\mycitemycitemycite[1]{%
  \edef\temp{(\noexpand\cite[\ArgI][\ArgII]{#1})}%
  \temp
}
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

\newcommand and optional parameters

Post by josephwright »

Looks like a job for xparse to me:

Code: Select all

\usepackage{xparse}
\NewDocumentCommand\mycite{O{}O{}m}{%
  (\cite[#1][#2]{#3})%
}
This avoids any \edef-worries.
Joseph Wright
kristianjohann
Posts: 5
Joined: Tue Oct 20, 2009 1:31 pm

Re: \newcommand and optional parameters

Post by kristianjohann »

Hello Sommerfee!

Thank you very much for your solution (2.) - it worked perfectly well!
Ad 1.: I tried both: [] and [\empty] and got the same result, namely an empty sign in the citation; so I thought that [] is internally implemented as [\empty].

Thanks again and have a nice evening, Christian
kristianjohann
Posts: 5
Joined: Tue Oct 20, 2009 1:31 pm

Re: \newcommand and optional parameters

Post by kristianjohann »

Hello josephwright!

Thank you very much for your solution - xparse also has done the job, but I tried to get a solution without loading an extra package.

Have a nice day, Christian
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

\newcommand and optional parameters

Post by localghost »

Now that the problem is solved, please be so kind and mark the topic (not the last post) accordingly as clearly written in the Board Rules (to be read before posting). Please keep that in mind for the future so that further reminders will not be necessary.


Thorsten
Post Reply