0

I programed a \NewDocumentCommand which produces vertical bars for each of which the numeric value and label text are mandatory and color as optional argument. There are also some global parameter (\startpoint, \scalingfactor, \bardistance, \barwidth etc.) that have to be specified prior to using the \vbarchart command (see MWE). This works well. However, I would like to know if someone has an idea how these "pre-parameters" can be prompted in a more inclusive way? Maybe producing a new environment and make them as prespecified options. The current way I would always have to copy these parameters to the section where I want to use the \vbarchart which is ok, but might be done better. Appreciate any help.

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usetikzlibrary{math}

\NewDocumentCommand{\vbarchart}{m m O{black!5}}{
    \tikzstyle{label1} = [align=center, above,pos=0.5]
    \tikzstyle{label2} = [align=center, below,yshift=-3pt]

    \draw[fill=#3] (\startpoint,0) --++(0,{#1*\scalingfactor}) --coordinate[pos=0.5](b1) node[label1,font=\percsize]{\pgfmathprintnumber[use comma]{#1}\Barunit} ++(\barwidth,0) -- ++(0,{-#1*\scalingfactor})coordinate(r1);

    \tikzmath{\startpoint=\startpoint+\bardistance+\barwidth;}
    \coordinate(h)(0,0);
    \node[label2,font=\labelsize] at (h-|b1){#2};
    \draw (-0.25,0) -- ([xshift=0.25cm]r1);
}


\begin{document}
    \begin{tikzpicture}
\tikzmath{\startpoint=0;}
\tikzmath{\scalingfactor=0.25;}
\tikzmath{\bardistance=0.5;}
\tikzmath{\barwidth=1;}
\newcommand{\percsize}{\normalsize}
\newcommand{\labelsize}{\normalsize}
\newcommand{\Barunit}{\%}

\vbarchart{20}{Bar\\ One}
\vbarchart{5}{Bar\\ One}[red]
\vbarchart{15}{Bar\\ Two}
\vbarchart{5}{Bar\\ One}[blue]
\end{tikzpicture}   
\end{document}
1
  • You can define those constants in the preamble, where you define \vbarchart, or you can define them in the definition of \vbarchart... Mar 12 '20 at 16:18
2

Sounds like a job for pgfkeys (see the Section Key management in the pgf manual). You can initialise keys with \pgfqkeys (or \pgfkeys) and then set the keys with the optional argument #3 to \vbarchart using a key1=value1,key2=value2,... syntax. I've marked my edits with comments in the code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xparse}
\usepackage{pgfkeys} % Load the package
\usetikzlibrary{math}

\NewDocumentCommand{\vbarchart}{m m O{}}{
    \pgfqkeys{/vbarchart}{% Define your keys - locally so that reset on every call of \vbarchart.
        barfill/.initial=black!5,
        percsize/.initial=\normalsize,
        labelsize/.initial=\normalsize,
        barunit/.initial=\%
    }
    \pgfqkeys{/vbarchart}{#3}% Set your keys according to the optional argument.
    \tikzstyle{label1} = [align=center, above,pos=0.5]
    \tikzstyle{label2} = [align=center, below,yshift=-3pt]

    % Then use the keys with \pgfkeysvalueof{/vbarchart/key}
    \draw[fill=\pgfkeysvalueof{/vbarchart/barfill}] (\startpoint,0) --++(0,{#1*\scalingfactor}) --coordinate[pos=0.5](b1) node[label1,font=\pgfkeysvalueof{/vbarchart/percsize}]{\pgfmathprintnumber[use comma]{#1}\pgfkeysvalueof{/vbarchart/barunit}} ++(\barwidth,0) -- ++(0,{-#1*\scalingfactor})coordinate(r1);

    \tikzmath{\startpoint=\startpoint+\bardistance+\barwidth;}
    \coordinate(h)(0,0);
    \node[label2,font=\pgfkeysvalueof{/vbarchart/labelsize}] at (h-|b1){#2};
    \draw (-0.25,0) -- ([xshift=0.25cm]r1);
}


\begin{document}
    \begin{tikzpicture}
\tikzmath{\startpoint=0;}
\tikzmath{\scalingfactor=0.25;}
\tikzmath{\bardistance=0.5;}
\tikzmath{\barwidth=1;}

\vbarchart{20}{Bar\\ One}[barunit=$\mu$,percsize=\huge]
\vbarchart{5}{Bar\\ One}[barfill=red]
\vbarchart{15}{Bar\\ Two}[labelsize=\footnotesize,percsize=\footnotesize]
\vbarchart{5}{Bar\\ One}[barfill=blue]
\end{tikzpicture}   
\end{document}

vbarchart

1
  • Thx Felix that was the missing link I needed. :)
    – kilsen
    Mar 13 '20 at 11:27
0

Based on Felix awnser I found the ultimate solution to my initial question. I used the pgfkeys logic and included them within a \NewDocumentEnvironment. This way I can now enter the needed values that are to be specified differently from the defaults and I don't have to copy the "constant definitions" everytime I want to draw a barchart. They are now just to be entered in the environment definition if needed.

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usepackage{pgfkeys}
\usetikzlibrary{shapes,shapes.geometric,arrows,fit,positioning,arrows.meta,angles,quotes,decorations.pathmorphing,decorations.shapes,decorations.text,calc,math}


\NewDocumentCommand{\vbarchart}{m m O{black!5}}{
    \tikzstyle{label1} = [align=center, above,pos=0.5]
    \tikzstyle{label2} = [align=center, below,yshift=-3pt]

    \draw[fill=#3] (\startpoint,0) --++(0,{#1*\scalingfactor}) --coordinate[pos=0.5](b1) node[label1,font=\percsize]{\pgfmathprintnumber[use comma]{#1}\Barunit} ++(\barwidth,0) -- ++(0,{-#1*\scalingfactor})coordinate(r1);

    \tikzmath{\startpoint=\startpoint+\bardistance+\barwidth;}
    \coordinate(h)(0,0);
    \node[label2,font=\labelsize] at (h-|b1){#2};
    \draw (-0.25,0) -- ([xshift=0.25cm]r1);
}

\NewDocumentEnvironment{barchartenv}{O{}}
{\pgfqkeys{/barchartenv}{
        scale/.initial=0.1,
        bardis/.initial=0.75,
        barwidth/.initial=0.75,
        percsize/.initial=\normalsize,
        labelsize/.initial=\normalsize,
        barunit/.initial=\%}
    \begin{tikzpicture}
    \pgfqkeys{/barchartenv}{#1}
    \tikzmath{\startpoint=0;}
    \tikzmath{\scalingfactor=\pgfkeysvalueof{/barchartenv/scale};}
    \tikzmath{\bardistance=\pgfkeysvalueof{/barchartenv/bardis};}
    \tikzmath{\barwidth=\pgfkeysvalueof{/barchartenv/barwidth};}
    \newcommand{\percsize}{\pgfkeysvalueof{/barchartenv/percsize}}
    \newcommand{\labelsize}{\pgfkeysvalueof{/barchartenv/labelsize}}
    \newcommand{\Barunit}{\pgfkeysvalueof{/barchartenv/barunit}}}
{\end{tikzpicture}}

\begin{document}

\begin{barchartenv}[scale=0.1,bardis=1,barwidth=1,percsize=\small,labelsize=\large,barunit={} Awnsers]
\vbarchart{25}{One}
\vbarchart{5}{Two}[red!50]
\vbarchart{20}{Three}
\vbarchart{35}{Four}[blue!50]

\end{barchartenv}

\end{document}

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.