2

I use a foreach loop to plot multiple curves via the \addplot command. The values that are necessary to define the relevant curve in each iteration are defined through some macros with the command \def. Each curve is also associated to a node text label that is supposed to show the value of a given macro at each iteration.

I manage to plot the curves correctly, whereas the node text label does not print out or it prints out some wrong number.

I have tried multiple possible solutions among the relatively few encountered online. However, I have not found a solution to this problem, which is almost certainly due to some parsing issue that I am unable to spot and resolve.

Please, find attached the minimum executable code to reproduce the image hereby. I would need the label to show the value of \YA in the equation $Y=\YA$.enter image description here

\documentclass[lulatex,beamer]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{frame}
\begin{center}
    
% Define parameters
\def\gammaA{0.5}
\def\gammaB{0.5}
\def\L{4}
\def\K{4}

\begin{tikzpicture}
\begin{axis}[
restrict x to domain=0:\L,
restrict y to domain=0:\K,
xmin = 0, xmax = \L,
ymin = 0, ymax = \K,
xlabel = {$L$},
ylabel = {$K$}
]

% Draw level curves
\foreach \i in {0.25, 0.5}{
    
    % Define variables as a function of \i
    \def\LA{\i*\L};
    \def\KA{(\gammaA*(1-\gammaB)*\K*\LA)/((1-\gammaA)*\gammaB*(\L-\LA)+\gammaA*(1-\gammaB)*\LA)};
    \def\YA{((\LA)^(1-\gammaA)*((\KA)^\gammaA)};
    
    % Plot for each \i
    \addplot [very thick, mark=none] {(\YA/(\x^(1-\gammaA)))^(1/\gammaA)} node [anchor=center, pos=0.8, fill=white, rounded corners=4pt, inner sep=1pt] {$Y=\YA$};
};
\end{axis}
\end{tikzpicture}

\end{center}    
\end{frame}
\end{document}
4
  • Loop in an axis requires a different approach.
    – JeT
    Commented May 15 at 13:17
  • Sure, that's why I asked for help. So, what is the different approach that would allow me to do what I want to do? Commented May 15 at 13:25
  • take a look at this post
    – JeT
    Commented May 15 at 13:53
  • 1
    I had already seen that and similar posts. Generally, they all deal with passing on the iteration variable (or some function thereof) as an argument to the coordinates command. But this is not what I need, and that is why I posted a minimum working example. So that any answer could move from there on. Commented May 15 at 14:07

1 Answer 1

6

The problem is that nodes are drawn after the plot, when the command \YA is no more defined.

You can play with expansion and define a command the draw the graph. Something like:

\edef\plotcommand
  {%
    \noexpand\addplot [very thick, mark=none]
    {(\YA/(\noexpand\x^(1-\gammaA)))^(1/\gammaA)}
    node [anchor=center, pos=0.8, fill=white, rounded corners=4pt, inner sep=1pt]
    {$Y=\YA$};
  }

Here \edef expands the definition before setting the command and \noexpand prevent the expansion of \addplot and \x. Replace then the \addplot command with \plotcommand.

By the way, I assume you're looking for the numerical value of the parameters, not their formulas. Replace \def with \pgfmathsetmacro in the computation:

\pgfmathsetmacro{\LA}{\i*\L};
\pgfmathsetmacro{\KA}{(\gammaA*(1-\gammaB)*\K*\LA)/((1-\gammaA)*\gammaB*(\L-\LA)+\gammaA*(1-\gammaB)*\LA)};
\pgfmathsetmacro{\YA}{((\LA)^(1-\gammaA)*((\KA)^\gammaA))};

Complete example:

\documentclass[lulatex,beamer]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{frame}
\begin{center}

% Define parameters
\def\gammaA{0.5}
\def\gammaB{0.5}
\def\L{4}
\def\K{4}

\begin{tikzpicture}
\begin{axis}[
restrict x to domain=0:\L,
restrict y to domain=0:\K,
xmin = 0, xmax = \L,
ymin = 0, ymax = \K,
xlabel = {$L$},
ylabel = {$K$}
]

% Draw level curves
\foreach \i in {0.25, 0.5}{

    % Define variables as a function of \i
    \pgfmathsetmacro{\LA}{\i*\L};
    \pgfmathsetmacro{\KA}{(\gammaA*(1-\gammaB)*\K*\LA)/((1-\gammaA)*\gammaB*(\L-\LA)+\gammaA*(1-\gammaB)*\LA)};
    \pgfmathsetmacro{\YA}{((\LA)^(1-\gammaA)*((\KA)^\gammaA))};

    % Plot for each \i
    \edef\plotcommand
    {%
      \noexpand\addplot [very thick, mark=none]
      {(\YA/(\noexpand\x^(1-\gammaA)))^(1/\gammaA)}
      node [anchor=center, pos=0.8, fill=white, rounded corners=4pt, inner sep=1pt]
      {$Y=\YA$};
    }
    \plotcommand
};
\end{axis}
\end{tikzpicture}

\end{center}
\end{frame}
\end{document}

Complete example

5
  • Thank you for your very useful answer. I've noticed that I have a further need relative to my initial question: namely, I would need to contro the font size of the node label. I have tried the standard way, but I get an error. Do you know how I could add a command like '\tiny' to make the label smaller? Commented May 16 at 10:24
  • 1
    @LauraRoich You need to use \noexpand to protect \tiny from expansion at command definition. Edit your question, if needed.
    – jlab
    Commented May 16 at 10:27
  • Apparently, I cannot edit my post anymore. I am unable to say why exactly. So probably you could just write here in a comment how the protection is supposed to take place. Or, if you have privileges, you can directly edit my question. Sorry about this, and thanks again. Commented May 16 at 10:46
  • 1
    @LauraRoich \noexpand prevent the expansion of the following token. Try \noexpand\tiny in place of \tiny.
    – jlab
    Commented May 16 at 10:48
  • 1
    \noexpand\tiny works perfectly. Many thanks! Commented May 16 at 10:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .