3

I define a command with one optional key parameter. This command makes some calculations and them draws a tikzpicture. How can made my command to accept all the keys that a \draw command accept and pass them?

\newlength{\gl@blgskip}
\pgfkeys{/lineskip/.cd,
                   .default=\baselineskip,
                   .store in = \gl@blgskip@macro
}


\newcommand{\baselinegrid}[1][]
{

   \pgfkeys{lineskip,#1}
   % set length to optional parameter lineskip
   \setlength{\gl@blgskip}{\gl@blgskip@macro}

  %%% Do some calculations ...


  \begin{tikzpicture}
    \draw (0,0) -- (0,5cm);
  \end{tikzpicture}

Now I can call my commands as \baselinegrid[lineskip=3cm]. I want to do thing like \baselinegrid[lineskiip=3cm, color=red] and pass the option color to the \draw command.

4

I'm not sure what you want to do exactly but I would recommend building the length-setting code into the keys themselves so you can pass all the arguments to the tikzpicture environment directly. Such as

\documentclass{article}
\usepackage{tikz}
\makeatletter
\newlength{\gl@blgskip}
\tikzset{lineskip/.cd,
   .default=\baselineskip,
   .code={\setlength{\gl@blgskip}{#1}}
}
\newcommand{\baselinegrid}[1][]
{
  \begin{tikzpicture}[#1]
    \draw (0,0) grid (\gl@blgskip,\gl@blgskip);
  \end{tikzpicture}
}
\makeatother
\begin{document}

\baselinegrid[lineskip=3cm,color=red]

\end{document}

sample code output

You don't need to put your keys in a separate namespace as long as you're not overwriting a tikz key. The .code key handler sets up a macro to handle the argument passed to it, so you don't have to .store in the argument and then call a separate macro later.

In situations such as these, I usually end up building all the code into keys such as the execute at begin/end picture so that your interface can be just \tikz[baselinegrid,lineskip=...]{}. If you explain more of what you want to see I can explain more.

  • This is what I needed! I have just a couple of things. First if the \tikzsetup is inside the \newcommand there is a compilation error, and second, if no lineskip option is given the value of \gl@blgskip is 0pt and not \baselineskip. (I fix this one with a \setlength command before the tikzpicture. – TeXtnik Apr 22 '13 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.