Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This is a completely noob question for LaTeX package writers. Could somebody share a piece of code or a MWE for the following situation. I would like to define a newcommand which will depend on the key. Something like

\newcommand{\mycommand}#if key_value=1 {test} else {test2}

which should do someting like follows

\mycommand[key=1]

should print test, otherwise the output should be test2.

I apologize to seasoned package writers for this and other noob questions. I am just overwhelmed with amount of simple material I am clueless about.

share|improve this question
5  
Will \mycommand have to distinguish between different keys? For example, do you allow inputs like \mycommand[house=1], \mycommand[tree=2], \mycommand[house=4,tree=1]? –  Werner May 31 '12 at 22:57
3  
I agree with Werner: the question is too generic for getting a sensible answer. If you need only one key, it's simpler not to use a key at all and use just an optional argument. –  egreg May 31 '12 at 23:05
    
@Werner No Werner.The key would have just to distinguish between two values of the single key. In particular I want a Syllabus to have an optional section called project. So if I type \coursecontent[project=yes] the command will put optional paragraph about project. If [project=no] the project section will be omitted. –  Predrag Punosevac Jun 1 '12 at 2:16
3  
In that case, despite my accepted answer below you don't need key=value syntax at all, the latex style would be just to have \coursecontent[yes] or since there are only two values have no argument at all and just \coursecontent and \coursecontent* (which you can code easily by using \def\coursecontent{\@ifstar{code for * version}{code for other version}} –  David Carlisle Jun 1 '12 at 8:15
    
@David Carlisle You are the best! Thank you so much. –  Predrag Punosevac Jun 1 '12 at 14:23

1 Answer 1

up vote 5 down vote accepted

There are several packages for defining a key=value syntax. I'll show keyval as it's part pf the basic latex distribution, and I know something about it.

If you LaTeX the following:

\makeatletter % not needed in a .sty file

\RequirePackage{keyval}




\define@key{test}{key}{%
  \count@=#1\relax}

\define@key{test}{color}{%
  \def\thiscolor{#1}}


\newcommand{\mycommand}[1]{%
  \count@=0 % default
  \def\thiscolor{}% default
  \setkeys{test}{#1}%
  \ifodd\count@
    \typeout{key=\the\count@: Odd!}%
  \else
    \typeout{key=\the\count@: Even!}%
   \fi
   \typeout{the color is \thiscolor}}



\typeout{======}
\mycommand{key=1,color=red]}

\stop

You will see both keys have been processed and the following typeouts are made

======
key=1: Odd!
the color is red]
 )
No pages of output.

The way this works is the package handles the splitting up of the comma separated settings , but for each key "key" and "color" here you have to define a command that does something with the value. Here the key is a number to be saved in \count@ and the colour is treated as text stored in \thiscolor, then after processing the keys these values can be used as normal TeX code.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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