3

I'm trying to make a command/environments with a view key value options similar to many other popular packages.

I found description on how to use key value but the examples don't work. Anyway can help to make a minimal key-value configured command my?

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{keyval}

\define@key{my}{foo}{Foo is #1\par}
\define@key{my}{bar}[99]{Bar is #1\par}

\newcommand{\KV@my@foo}[1]{Foo is #1}

\begin{document}

\my{foo=3,bar}

\end{document}

enter image description here

Luman75
  • 225
  • 1
    I don't know the keyval package but if you want to use commands with an @ in their names you at least must make @ an letter before you use them, see https://tex.stackexchange.com/questions/8351/ – cgnieder May 06 '20 at 08:58
  • Please note that nowadays there might be packages which are more robust and/or easier to use than keyval. IMHO, the three packages which should be considered nowadays to create new stuff are: l3keys/LaTeX-kernel key=value support (stable, built in), expkv (with expkv-def or expkv-cs -- disclaimer: I'm the author; stable, fast, in case of expkv-cs very easy for simple things), or pgfkeys (wide spread, versatile, flexible at use-time -- the last might be considered an advantage and a drawback). – Skillmon Jul 30 '23 at 21:01

1 Answers1

5

enter image description here

defining keys is usually in a package so guarded with @ but you can use \makeatletter to write package code in the preamble.

\documentclass{article}

\usepackage{keyval}

\makeatletter
\define@key{my}{foo}{Foo is #1\par}
\define@key{my}{bar}[99]{Bar is #1\par}

\makeatother

\newcommand\my[1]{\setkeys{my}{#1}}

\begin{document}

\my{foo=3,bar}

\end{document}
David Carlisle
  • 757,742