[Tex/LaTex] tikzpicture as \newcommand with argument(s)

macrostikz-pgf

I have a tikz barplot which I want as a \newcommand with a variable. Depending on the variable, labels should be added or not. In the document I will use the plot several times, sometimes with, sometimes without labels. Thats why I want it as a command.

here my MWE:

\documentclass{article}
\usepackage{tikz, pgfplots}
\begin{document}

\newcommand{\myplotWithoutLabels}{
\begin{tikzpicture}
\begin{axis}[xbar, bar shift=0pt, bar width=20pt, xmin=0,
axis x line = none, axis y line* = middle, 
ytick={1,2,3}, tickwidth=0, every tick/.style={draw=none},% label pos, no   tick marks
yticklabels={}
]
\addplot coordinates {(2,1)};
\addplot coordinates {(5,2)};
\addplot coordinates {(4,3)};
\end{axis}
\end{tikzpicture}
}

\myplotWithoutLabels

\newcommand{\myplotWithLabels}{
\begin{tikzpicture}
\begin{axis}[xbar, bar shift=0pt, bar width=20pt, xmin=0,
axis x line = none, axis y line* = middle, 
ytick={1,2,3}, tickwidth=0, every tick/.style={draw=none},
yticklabels={L1, L2, L3}
]
\addplot coordinates {(2,1)};
\addplot coordinates {(5,2)};
\addplot coordinates {(4,3)};
\end{axis}
\end{tikzpicture}
}

\myplotWithLabels
\end{document}    

I've tried several things, i.e.:

\newcommand{\myplot}[3]{
\begin{tikzpicture}
\begin{axis}[title=my Title,
xbar, bar shift=0pt, bar width=20pt, xmin=0,
axis x line = none, axis y line* = middle,
ytick={1,2,3}, tickwidth=0, every tick/.style={draw=none},
yticklabels={#1, #3, #3}
]
\addplot coordinates {(2,1)};
\addplot coordinates {(5,2)};
\addplot coordinates {(4,3)};
\end{axis}
\end{tikzpicture}
}

\myplot[L1,L2,L3]

or with only one (#1) argument. Neither did work.

The solution to this question Problem declaring newcommand with tikzpicture inside didn't help me either.

EDIT:
Thanks for the comments and answers. Now the \newcommand works. However, there is one problem left. The plot generated with the \newcommand with argument is slightly shifted up on the y axis.

Here the code, I've put the two plots next to each other using subcaption package (\usepackage{subcaption}) to see the difference. The plots are both as \newcommand, one without argument, one with argument.

\newcommand{\myplot}[1]{
\begin{tikzpicture}
\begin{axis}[
xbar, bar shift=0pt, bar width=20pt, xmin=0,
axis x line = none, axis y line* = middle,
ytick={1,2,3}, tickwidth=0, every tick/.style={draw=none},
yticklabels={#1}
]
\addplot coordinates {(2,1)};
\addplot coordinates {(5,2)};
\addplot coordinates {(4,3)};
\end{axis}
\end{tikzpicture}
}

\begin{figure}[h!]
\centering
\begin{subfigure}[h!]{0.45\textwidth}
\myplotWithoutLabels
\end{subfigure}
\begin{subfigure}[h!]{0.45\textwidth}
\myplot{}
\end{subfigure}
\end{figure}

plots next to each other

I can't find the reason for the shift.

Best Answer

You have two choices; which one depends very much on the application.

Remember that if you do

\newcommand{\foo}[3]{...}

then a call to \foo should be of the form

\foo{first}{second}{third}

where each argument is braced. It's not the same as in other programming language: the syntax \foo{first,second,third} would take first,second,third as #1 and TeX would look further for #2 and #3.

\documentclass{article}
\usepackage{tikz, pgfplots}

\newcommand{\myplotA}[3]{%
  \begin{tikzpicture}
  \begin{axis}[
    xbar,
    bar shift=0pt,
    bar width=20pt,
    xmin=0,
    axis x line = none,
    axis y line* = middle, 
    ytick={1,2,3},
    tickwidth=0,
    every tick/.style={draw=none},% label pos, no   tick marks
    yticklabels={#1,#2,#3},
  ]
  \addplot coordinates {(2,1)};
  \addplot coordinates {(5,2)};
  \addplot coordinates {(4,3)};
  \end{axis}
  \end{tikzpicture}%
}

\newcommand{\myplotB}[1]{%
  \begin{tikzpicture}
  \begin{axis}[
    xbar,
    bar shift=0pt,
    bar width=20pt,
    xmin=0,
    axis x line = none,
    axis y line* = middle, 
    ytick={1,2,3},
    tickwidth=0,
    every tick/.style={draw=none},% label pos, no   tick marks
    yticklabels={#1},
  ]
  \addplot coordinates {(2,1)};
  \addplot coordinates {(5,2)};
  \addplot coordinates {(4,3)};
  \end{axis}
  \end{tikzpicture}%
}

\begin{document}

\myplotA{L1}{L2}{L3}

\bigskip

\myplotB{L1,L2,L3}

\end{document}    

As you see in the picture, the output is the same.

enter image description here

Related Question