Simple macros like these can be created with expkv-cs
quite comfortably.
The \ekvcSplit
macro sets up a command that takes one mandatory argument (hence we use a \NewDocumentCommand
-defined wrapper for the optional argument), and splits it into separate arguments in the order of the defined primary keys, so that you can use #1
for the first key in the definition. Additional keys that don't correspond to arguments directly can be set up using \ekvcSecondaryKeys
(which we can use to set up your flag-like keys, which will then just forward specific values to your underlying keys).
\documentclass{article}
\usepackage{expkv-cs}
\makeatletter
% grabbing argument as an optional argument
\NewDocumentCommand\VerticalStep{O{}}{\VerticalStep@{#1}}
\ekvcSplit\VerticalStep@
{
length = 0pt % value doesn't matter, will be set by ekvcChange
,sign = {} % empty for positive, else -
,star = {}
}{\vspace#3{#2#1}}
\ekvcSecondaryKeys\VerticalStep@
{
nmeta negative = {sign=-}
,nmeta small = {length=1cm}
,nmeta standard = {length=2cm}
,nmeta force = {star=*}
}
\ekvcChange\VerticalStep@{standard}
\makeatother
\begin{document}
abc
\VerticalStep
def
\VerticalStep[negative]
ghi
\VerticalStep[small,negative]
jkl
\VerticalStep[length=5cm]% arbitrary sizes also possible
mno
\end{document}
Another solution using a more traditional key=value interface as provided by expkv-def
. This uses a skip-register to store the length, the standard
and small
keys change the length in that register, and the negative
-key works as a Boolean (so can be set true or false).
\documentclass{article}
\usepackage{expkv-def}
\makeatletter
\ekvdefinekeys{VerticalStep}
{
skip length = \VerticalStep@length
,nmeta standard = {length = 2cm}
,initial standard
,nmeta small = {length = 1cm}
,boolTF negative = \VerticalStep@negative
}
\ekvsetdef\VerticalStepSetup{VerticalStep}
\NewDocumentCommand\VerticalStep{O{}}
{%
\begingroup
\VerticalStepSetup{#1}%
\vspace{\VerticalStep@negative{-}{}\VerticalStep@length}%
\endgroup
}
\makeatother
\begin{document}
abc
\VerticalStep
def
\VerticalStep[negative]
ghi
\VerticalStep[small,negative]
jkl
\VerticalStep[length=5cm]% arbitrary sizes also possible
mno
\end{document}
Outputs of both look identical:
define@boolkey
, but examples on the site are sparse e.g. macros - How to define a key=value that has an equivalent boolean value key - TeX - LaTeX Stack Exchange.keyval
, you just need to give some default argument to allow omitting the value (see texdoc keyval)