Tag Info

Hot answers tagged

69

Use pgfkeys! There are three steps to this: first, you must make your command accept keys as options. Traditionally, this is as you wrote it: an optional argument. In that case, you should start your definition like this: \newcommand\myparbox[2][]{% \pgfkeys{#1}% ... } The syntax is that the optional argument is expected to contain a list of keys ...


23

Here is a short example of how to use keys by virtue of the keyval package. The steps you need to follow are all based on the following macro: \define@key{<family>}{<key>}{<function>} Define your <family>: Use the above command by choosing some <family> name that all the keys will be associated with. In my example below, I ...


22

In general, the order of keys does matter where there is some interaction between the keys. That is because keys are normally processed in a left-to-right sense, and so the settings from the 'earlier' keys can be altered by those 'later' in the list. There are cases where internally implementations use multiple pass approaches to setting keys. This is done ...


20

The kvoptions package gives you the possibility to define key=value style options. It "connects package keyval with LATEX's package and class options" (quote from the manual). You can then declare options like margin using: \DeclareStringOption [<init>]{<key>}[<default>] were <init> is the initial value (also used when the option ...


17

This is a good thing, since it promotes consistency. However, your key-value argument needs to be expanded before it can be properly assigned using key-value pairs. For this you have at least two options: Explicitly insert \expandafters to jump over the construction until you reach the optional argument(s): ...


16

Accepting key-value input can be done using a number of packages, and the general approach is the same for all of them: I covered this in some detail in a TUGboat article. Essentially, there are three things you need to do Define one or more keys; Tell LaTeX to process package options using these keys; Provide a macro for setting keys after package ...


16

Let me provide a simple (but full) example: \ProvidesPackage{myemph}[2011/03/12 v1.0 a test package] \providecommand\my@emphstyle{\em} % Note that the argument must be expandable, % or use xkvltxp package before \documentclass (see manual of xkeyval) \RequirePackage{xkeyval} \DeclareOptionX{style}{% \def\my@emphstyle{\csname my@style@#1\endcsname}} % ...


15

The order is only important if the keys depend to each other. dx is a PSTricks length and it can be used with or without a unit. Without a unit it takes the current value of psxunit into account! And that value is set by the key xunit. That is the expected behaviour of a relative length setting! Using dx with an absolute length (has a unit like \psPiH cm) ...


12

pgfkeys.sty, part of PGF/TikZ Author: Till Tantau


12

kvoptions.sty Author: Heiko Oberdiek Supports keyval processing of package/class options.


12

A possible solution: \documentclass[border=5mm]{standalone} \usepackage{tikz} \pgfkeys{/tikz/.cd,% to set the path circle height/.initial=0, % initial value circle height/.get=\circleheight, % to get the value from a macro circle height/.store in=\circleheight, % to store the value into a macro diameter/.initial=1, diameter/.get=\diameter, ...


12

If you want all the options to your package to be passed to hyperref then you can just do \DeclareOption*{\PassOptionsToPackage{\CurrentOption}{hyperref} \ProcessOptions\relax \RequirePackage{hyperref} see clsguide.tex documentation in the LaTeX base. I just read clsguide:-) and if this is all you want to do, it points out that there is a more efficient ...


11

The width key of graphicx sets a macro which then stays valid for the rest of the scope, while scale modifies the internal token register used to accumulate the required code for the current image. This token register is reset at the begin of the internal \includegraphics code and therefore any previous changes are lost. Anyway, the order of the keys and the ...


11

Value keys (created by the /.initial handler) save the value in a macro named \pgfk@<full path to key>. Most used examples probably include the keys minimum width, minimum height as well as the inner and outer separations that are read very often while PGF constructs a node. Besides directly using \csname pgfk@<full path to key> they can be ...


11

My personal preference is to avoid using .store in keys at all, with the following exception: A .store in key is required when using pgfkeys as an interface to an existing system that doesn't use it. For example, suppose that you wanted, in the course of your key-processing, to alter the material that would be printed by \ref if it pointed to a \label ...


10

A possible solution with LaTeX3 key-value interface: tmpsty.sty \NeedsTeXFormat{LaTeX2e} \ProvidesPackage{tmpsty}[2013/09/24 Standard Style File For Assessments At SHJC] \RequirePackage[letterpaper,tmargin=2cm,bmargin=2cm,lmargin=2.5cm,rmargin=2.5cm,showframe]{geometry} \RequirePackage{amsmath,amssymb,amsfonts} \RequirePackage{booktabs,array} ...


10

The problem is that when verbatim-style environments take optional arguments, you need to use \obeylines to prevent the body of the environment from being tokenized in the process of checking for arguments. You should be able to pass options to minted, though if you're primarily working with formatting, passing options directly to \fvset might be easier. ...


9

They're for two different things: A key defined using /.initial=<value or string> is a value-storing key, with the initial value set to <value or string>. The /.default keyword defines what value will be used as the argument for a key defined with /.code=<code> if no argument is provided. The two things are similar, but not equivalent. A ...


9

Here is a very elementary view on LaTeX's key-value system using keyval, similar to what was presented in How to create a command with key values?: \documentclass{article} \usepackage{keyval,xparse}% http://ctan.org/pkg/{keyval,xparse} \makeatletter % ========= KEY DEFINITIONS ========= \define@key{mymacro}{first}{\def\mm@first{#1}} ...


9

keyval.sty Author: David Carlisle The original and (probably) most widely used keyval package. Provides \define@key and \setkeys but all processing for special key-types must be done manually. Removes one or two layers of braces around each "val". Note: this package can have scope problems with nesting \setkeys. Nowadays I'd recommend a more modern ...


9

As noted at here, \setkeys from the keyval package may not be nested. This is because it doesn't push any current state before commencing process. Besides this, we no longer have to repeatedly call \define@key to define several keys. Here is a key command approach. \documentclass{article} \usepackage{xcolor} \usepackage{ltxkeys} \makeatletter % To avoid ...


8

l3keys.sty, part of expl3 Author: Joseph Wright (through the LaTeX3 Project) Inspired by pgfkeys. Use l3keys2e to use it in options of LaTeX2e packages and classes.


8

The keyval approaches all avoid expanding input, so in your \lstset{\flags} example the code sees \flags and not the content of the macro. The error message here is a bit unhelpful as it is expanded by TeX, so it looks like what you expect! You need to expand the input before applying the keyval macro \documentclass{minimal} \usepackage{listings} ...


8

Although any key-value system gives the appearance of "setting properties" of some kind (for example, the numerical parameters of a picture), in fact to some extent they are all "calling methods" instead. I really only know about pgfkeys, which is not what you're asking about, but in that system, keys are as powerful as TeX macros and, therefore, can have ...


8

Solution using \comma@parse of kvsetkeys: \NeedsTeXFormat{LaTeX2e}[1994/12/01] \ProvidesPackage{package}[2012/09/18 Example] \RequirePackage{keyreader} \RequirePackage{kvsetkeys} \DeclareOptionX*{% \PackageWarningNoLine{}{Cant understand option(s): \CurrentOption}% } \def\TempDefOption#1{% \krdDeclareOption{#1}{\PassOptionsToPackage{##1}{#1}}% } ...


8

You have not given the c-type argument in the correct form: it should (in general) not start with a \ but should be in braces: \prop_map_inline:Nn \l_my_prop { \keys_define:nn { phylogenetictree } { #1 .tl_set:c = { l_ #1 _tl } } } I've also changed .tl_gset:c to .tl_set:c here, as your variable name (\l_...) indicates that it ...


8

The \ifthenelse tests are not expandable. You can use \pdfstrcmp for the comparison (and \pdf@strcmp is better because it's engine independent): \documentclass{article} \usepackage{etoolbox,pdftexcmds} \begin{document} \makeatletter \def\foo@bar{value} \def\thing@i#1{% \thing@ii#1&} \def\thing@ii#1=#2&{% \ifnum\pdf@strcmp{#1}{form}=\z@ ...


8

The \ifcommandkey approach There is a bug in the definition of \ifcommandkey (2010/04/27 v3.1415). It is missing out one stage of expansion of the \commandkey (see the analysis in the section below), and as a result does not test the value of the key. (You will get TRUE even with an undefined key.) Try the following definition in the preamble after loading ...


8

A slightly modified interface via the definition \newcommand{\happygraphics}[2][,]{% \includegraphics[width=50,height=25,trim=1 2 3 4,clip,#1]{#2}}% would allow to add options as needed to \happygraphics[<options>]{<file>}. A similar interface is provided through xparse: \usepackage{xparse}% http://ctan.org/pkg/xparse ...


8

The reason is the definition of the key. The value of the key isn't expanded and the resulting test fails. You can use the following redefinition: \makeatletter \define@key[psset]{pstricks}{showgrid}[b]{\expandafter\pst@@showgrid#1\@nil} \makeatother The expansion above works only once. To expand the value of the key you can use: \makeatletter ...



Only top voted, non community-wiki answers of a minimum length are eligible