LaTeX forum ⇒ GeneralGlobal and Individual Definition of Key Options

LaTeX specific issues not fitting into one of the other forums of this category.
Lady Jamcrackers
Posts: 3
Joined: Wed Aug 28, 2013 11:16 pm

Global and Individual Definition of Key Options

Postby Lady Jamcrackers » Tue Jan 28, 2014 1:49 am

I am using xkeyval to write a TikZ-like environment and set of commands to be used in that environment. I would like the user to be able to set an option globally, i.e.
  1. \begin{myenvironment}[myoptiona=blue, myoptionb=20pt]

or when using each instance of a certain command, i.e.
  1. \mycommand[myoptiona=blue]
  2. \mycommand[myoptiona=green]

In the MWE below, I ideally want the first bit of code to produce a blue line, but it in fact produces a red line instead. The problem is that when \mycommand is implemented without the user setting myoptiona, that key reverts to the preset value in \presetkeys{mycommand}{<preset values>} instead of keeping the value set globally by the user at the beginning of the environment. However, I want the second bit of code to work exactly the way it does, so there need to be preset values of some sort. What is the proper way to fix this? (I don't want an awkward workaround.)

  1. \documentclass{minimal}
  2.  
  3. \usepackage{tikz}
  4. \usepackage{color}
  5. \usepackage{xkeyval}
  6.  
  7. \makeatletter
  8.  
  9. \define@key{myenvironment}{myoptiona}{\def\myoptionadef{#1}}
  10. \define@key{myenvironment}{myoptionb}{\def\myoptionbdef{#1}}
  11. \define@key{mycommand}{myoptiona}{\def\myoptionadef{#1}}
  12.  
  13. \savekeys{myenvironment}{myoptiona, myoptionb}
  14. \savekeys{mycommand}{myoption}
  15.  
  16. \presetkeys{myenvironment}{myoptiona=red, myoptionb=10pt}{}
  17. \presetkeys{mycommand}{myoptiona=red}{}
  18.  
  19. \makeatother
  20.  
  21. \newenvironment{myenvironment}[1][]{%
  22. \begin{tikzpicture}
  23. \setkeys{myenvironment}{#1}
  24.  
  25. \draw (2,0) coordinate circle (\myoptionbdef);
  26. }{\end{tikzpicture}}
  27.  
  28. \newcommand{\mycommand}[1][]{%
  29. \setkeys{mycommand}{#1}
  30. \draw[color=\myoptionadef] (0,0) -- (1,0);
  31. }
  32.  
  33. \begin{document}
  34.  
  35. This example produces a red line, but I want it to produce a blue line:
  36.  
  37. \begin{myenvironment}[myoptiona=blue, myoptionb=20pt] % myoptiona has been globally set to blue.
  38. \mycommand % myoptiona gets set back to the preset value red because no key value was set by user.
  39. \end{myenvironment}
  40.  
  41. This example works the way I want it to:
  42.  
  43. \begin{myenvironment}[myoptionb=20pt] % I want the user to be able to omit setting myoptiona.
  44. \mycommand
  45. \end{myenvironment}
  46.  
  47. \end{document}
Last edited by localghost on Tue Jan 28, 2014 10:47 am, edited 1 time in total.
Reason: Source code has to be tagged as such to keep a post clear and legible (see Board Rules).

Tags:

Return to “General”

Who is online

Users browsing this forum: No registered users and 4 guests