Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am defind my own command to plat a diagram with tikz.

Instead of constant values I rather want to pass numbers to my command which determine the height of the bars in my diagram.

Problem: I am using a wrong syntax, so Tex cannot parse my arguments to set the bar height. I tried a, #a, {#a} and #{a} but nothing workd so far.

Question: What is my mistake and how can I solve it?

\newcommand{\clocksTen}[a][b][c][d]{
\begin{tikzpicture}
%  \centering
\begin{axis}[
ybar,
enlargelimits=0.15,
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=-1},
%width=0.4\textwidth,
ylabel={CPU clocks},
symbolic x coords={one, two, three, four},
xtick=data,
nodes near coords,
nodes near coords align={vertical},
]
\addplot coordinates {
(one, a) %doesnt work
};
\addplot coordinates {
(two, #a) %doesnt work
};
\addplot coordinates {
(three, {#a}) %doesnt work
};
\addplot coordinates {
(four, #{a}) %doesnt work
};
\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}
}
share|improve this question

1 Answer 1

up vote 2 down vote accepted

LaTeX arguments are always numeric, preceded by a hash sign, e.g. #1. You provide the number of required parameters (later entered inside curly braces) to \newcommand in square brackets. The default value for a single optional parameter (always #1) may follow in another pair of square brackets and is put inside square brackets when used, too.

\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgfplots}

\newcommand{\clocksTen}[4]{
\begin{tikzpicture}
\begin{axis}[
  ybar,
  enlargelimits=0.15,
  legend style={at={(0.5,-0.15)},
  anchor=north,legend columns=-1},
  ylabel={CPU clocks},
  symbolic x coords={one, two, three, four},
  xtick=data,
  nodes near coords,
  nodes near coords align={vertical},
]
\addplot coordinates {(one, #1)};
\addplot coordinates {(two, #2)};
\addplot coordinates {(three, #3)};
\addplot coordinates {(four, #4)};
\legend{one, two, three, four}
\end{axis}
\end{tikzpicture}
}
\begin{document}

\clocksTen{2}{4}{8}{16}

\end{document}

PS: Please always add document class and necessary packages to your MWE so it’s actually working.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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