Generalkeyval

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
pjhlatexuser
Posts: 1
Joined: Tue Dec 17, 2013 4:15 pm

keyval

Post by pjhlatexuser »

I am trying to learn and understand how to use 'keyval', and ran across these two references:
http://www.tex.ac.uk/cgi-bin/texfaq2html?label=keyval
and
http://www.texdev.net/wp-content/upload ... keyval.pdf

Per those guidelines, I attempted to test 'keyval' with the following code.

Code: Select all

\documentclass{article}
\usepackage{keyval}
\define@key{my}{foo}{Foo is #1}
\begin{document}
\end{document}
However, the following error is reported back to me in the TeXworks console output:

Code: Select all

This is pdfTeX, Version 3.1415926-2.4-1.40.13 (MiKTeX 2.9 64-bit)
entering extended mode

("C:/Users/first.last/Documents/LaTeX & Word/LaTeX_Test_Files/testkeyval2.te
x"
LaTeX2e <2011/06/27>
Babel <v3.8m> and hyphenation patterns for english, afrikaans, ancientgreek, ar
abic, armenian, assamese, basque, bengali, bokmal, bulgarian, catalan, coptic, 
croatian, czech, danish, dutch, esperanto, estonian, farsi, finnish, french, ga
lician, german, german-x-2012-05-30, greek, gujarati, hindi, hungarian, iceland
ic, indonesian, interlingua, irish, italian, kannada, kurmanji, latin, latvian,
 lithuanian, malayalam, marathi, mongolian, mongolianlmc, monogreek, ngerman, n
german-x-2012-05-30, nynorsk, oriya, panjabi, pinyin, polish, portuguese, roman
ian, russian, sanskrit, serbian, slovak, slovenian, spanish, swedish, swissgerm
an, tamil, telugu, turkish, turkmen, ukenglish, ukrainian, uppersorbian, usengl
ishmax, welsh, loaded.
("C:\Program Files\MiKTeX 2.9\tex\latex\base\article.cls"
Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
("C:\Program Files\MiKTeX 2.9\tex\latex\base\size10.clo"))
("C:\Program Files\MiKTeX 2.9\tex\latex\graphics\keyval.sty")
! Undefined control sequence.
l.5 \define
           @key{my}{foo}{Foo is #1}
?}
What is the source of the error (i.e., what is the appropriate way to define a key?

Thank you.

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

keyval

Post by sommerfee »

pjhlatexuser wrote:What is the source of the error (i.e., what is the appropriate way to define a key?
Commands with a @ in their command name like \define@key are designed not to be used within documents. So either put that stuff into a separate package file and input it with \usepackage from the main document, or use \makeatletter and \makeatother around this code, e.g.:

Code: Select all

\documentclass{article}
\usepackage{keyval}
\makeatletter
\define@key{my}{foo}{Foo is #1}
\makeatother
\begin{document}
\end{document}
See also: http://www.latex-community.org/viewtopic.php?f=5&t=3732
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

keyval

Post by Johannes_B »

The symbol @ has a special meaning for latex. You need to put \makeatletter and \makeatother aroud it. The @ acts just like any regular letter (a,b,c ...) in between them.

Code: Select all

    
\documentclass{article}
\usepackage{keyval}
\makeatletter
\define@key{my}{foo}{Foo is #1}
\makeatother
\begin{document}
\end{document}
Hint: You don't have to do this in package files (file extension .sty).

You might want to have a look at the l3packages. The l3kernel still experimental, but considered as stable.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply