[Tex/LaTex] How to pass a variable to a command in LaTeX

macrospgfmathvariable

I am trying to make a tax calculator for an invoice template and the idea is to pass the variable subcost to the command \taxcalc.

\newcommand*{\taxcalc}[2]{%
  \pgfmathparse{#1/(1.0-#2)-#1}%
  \pgfmathprintnumber{\pgfmathresult}%
}

\newcommand{\vatTotal}{
     & & & {\bf BTW 21\%} & {\bf {\bf \euro\taxcalc{subcost}{0.21}}}
    \\*[1.5ex]
}

The problem is, no matter what I try to do to subcost, the compiler keeps complaining:

Package PGF Math Error: Unknown function `subcost' (in 'subcost/(1.0-0.21)-subcost').'

It works perfectly for the following:

\newcommand*{\total}[1]{\FPdiv{\t}{\arabic{#1}}{1000}\formatNumber{\t}}

\newcommand{\subTotal}{
    & & & {\bf Subtotaal (excl. btw)} & {\bf \euro\total{subcost}}
    \\*[1.5ex]
}

Can anyone help me? I've been googling for hours now in order to find how to correctly pass variables to functions in this language but no matter what I try, it only works if I hardcode a number.

subcost is defined as follows:

\newcounter{hours} \newcounter{subhours}
\newcounter{cost} \newcounter{subcost}
\newcounter{vat}

Best Answer

Your approach works. You can declare a variable, or parameterless function, with pgf which you seem to be using. In order to be able to redefine the function, you need the starred version, \pgfmathdeclarefunction*. This allows you to define a variable and assign it a value which you can use in any expression that you parse with pgf. For your convenience, I packed the \pgfmathdeclarefunction* stuff in a macro \SetPgfVariable.

\documentclass{article}
\usepackage{pgf}
\newcommand*{\taxcalc}[2]{%
  \pgfmathparse{#1/(1.0-#2)-#1}%
  \pgfmathprintnumber{\pgfmathresult}%
}
\newcommand{\SetPgfVariable}[2]{%
\pgfmathdeclarefunction*{#1}{0}{\pgfmathparse{#2}}}
\begin{document}
\SetPgfVariable{subcost}{0.4}
\taxcalc{subcost}{0.21}

\SetPgfVariable{subcost}{0.5}
\taxcalc{subcost}{0.21}
\end{document}

enter image description here

Related Question