When you write
\newcommand{\iterate}[2]{
\directlua{
for i=1,#1,1 do
tex.print(\\compare{i}{#2})
end
}
}
The Lua process sees
for i=1,#1,1 do
tex.print(<the result of \compare>)
end
What you need to do is something like that:
\newcommand\iterate[2]{%
\luaexec{
for i=1,#1 do
tex.print(string.format("\\compare{\%d}{#2}",i))
end
}
}
\iterate{3}{2}
(luaexec is from the luacode package) This prints "\compare{1}{2}
\compare{2}{2}
\compare{3}{2}
" into TeX's input string and that gets evaluated. This works because \\
generates a backslash and it is separate from the word "compare". It's a hack and I don't recommend that. Please see How to do a 'printline' in LuaTeX for more info.
This is an experiment on "reevaluating":
For example: if you write
\documentclass[a4paper]{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
tex.sprint("%hello")
\end{luacode*}
\end{document}
TeX will see %hello
and interprets it as a comment and thus no output file will be generated.
When you write this instead (see the -2 as the first argument to tex.print())
\documentclass[a4paper]{article}
\usepackage{luacode}
\begin{document}
\begin{luacode*}
tex.sprint(-2,"%hello")
\end{luacode*}
\end{document}
tex sees %hello, but the % has a "safe" category code. If the first argument to tex.print is a number, it will be taken as a catcode table. What I want to show: TeX reads the result of the \directlua{}
call.
As already explained in some comment, \pgfmathsetmacro{72400}
is unsupported by PGF (in fact, my system accepts it without problems - apparently something has changed in PGF CVS).
Nevertheless, you do not need \pgfmathsetmacro
just to declare a constant; it is much simpler to write \def\MACRO{<constant>}
(or use \newcommand\MACRO{<constant>}
which should be the same).
Then you need to assign a domain
. The key(s) restrict * to domain
are no definition how to sample points; they can be used to exclude already sampled points from the region of interest. In your case, you would define domain=775
and omit the restrict * to domain
.
Finally, math expressions in parametric plots need extra curly braces if they contain other round braces. In other words, use ({x/\modulus+0.002*(x/\yield)^15},x)
to avoid confusion with the round braces (TeX cannot automatically balance them, it can only balance curly braces).
Taking this together, I arrive at the following modification of your first plot:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\pgfplotsset{stressstrainset/.style={%
axis lines=center,
xlabel={$\varepsilon$},
ylabel={$\sigma$},
%restrict x to domain=0:15,
domain=0:775,
xmin=0.0, xmax= 15,
ymin=0.0, ymax= 775,
samples=100,
}}
\begin{tikzpicture}
\def\modulus{72400}
\def\yield{325}
\begin{axis}[stressstrainset]
\addplot[black] ({x/\modulus+0.002*(x/\yield)^15},x);
\end{axis}
\end{tikzpicture}
\end{document}
Best Answer
If you modify the function to
then the plot plots the (wrong:-) function without error but you get to see what you were plotting. The first few values are OK but then:
Presumably pgf isn't expecting the
e
notation. Presumably lua has some number formatting functions to prevent it using that notation?edit ah yes: