This is a follow up question on Key names of the axis limits when not set explicitly. The solution as given there works great for regular PGFplots axis environment, but not for a loglog axis enviroment. The \pgfkeysgetvalue{/pgfplots/...}{...} commands within the \pgfplotsextra do not seem to work properly in case of a loglog axis. How do I fix this?
Compiling the code
\documentclass[margin=1cm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\newcommand{\logLogSlopeTriangle}[9]
{
% #1. Relative offset in x direction.
% #2. Width in x direction, so xA-xB.
% #3. Relative offset in y direction.
% #4. Slope d(y)/d(log10(x)).
% #5. Plot options.
% #6. xmin.
% #7. xmax.
% #8. ymin.
% #9. ymax.
\pgfplotsextra
{
% Calculate auxilliary quantities.
\pgfmathsetmacro{\xA}{#6^(1-#1)*#7^#1}
\pgfmathsetmacro{\yA}{#8^(1-#3)*#9^#3}
\pgfmathsetmacro{\xB}{#6^(1-(#1-#2))*#7^(#1-#2)}
\pgfmathsetmacro{\yB}{\yA}
\pgfmathsetmacro{\xC}{\xA}
\pgfmathsetmacro{\yC}{\yA/(\xB^#4)*\xA^#4}
% Define coordinates for \draw.
\coordinate (A) at (axis cs:\xA,\yA);
\coordinate (B) at (axis cs:\xB,\yB);
\coordinate (C) at (axis cs:\xC,\yC);
% Draw slope triangle.
\draw[#5] (A)-- node[pos=0.5,anchor=north] {1}
(B)--
(C)-- node[pos=0.5,anchor=west] {#4}
cycle;
}
}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}
[
xmin=10^1, % WHAT IF I DON'T USE THIS?
xmax=10^4, % WHAT IF I DON'T USE THIS?
xlabel=$x$,
ymin=10^0.5, % WHAT IF I DON'T USE THIS?
ymax=10^4, % WHAT IF I DON'T USE THIS?
ylabel style={rotate=-90},
ylabel=$y$,
grid=major,
clip=false
]
\addplot[blue,line width=1pt,domain=10^1:10^4] {sqrt(x)};
\addplot[red,line width=1pt,domain=10^1:10^4] {x};
%\pgfplotsextra
%{
\pgfkeysgetvalue{/pgfplots/xmin}{\xmin}
\pgfkeysgetvalue{/pgfplots/xmax}{\xmax}
\pgfkeysgetvalue{/pgfplots/ymin}{\ymin}
\pgfkeysgetvalue{/pgfplots/ymax}{\ymax}
%}
\logLogSlopeTriangle{0.9}{0.1}{0.1}{1/2}{blue}{\xmin}{\xmax}{\ymin}{\ymax};
\logLogSlopeTriangle{0.75}{0.1}{0.1}{1}{red}{\xmin}{\xmax}{\ymin}{\ymax};
\end{loglogaxis}
\end{tikzpicture}
\end{document}
I obtain the desired result

In case I do not want to explicitly specify the axis limits in the axis options, using \pgfplotsextra as desrcibed in Key names of the axis limits when not set explicitly should offer a solution.
Accordingly compiling the code
\documentclass[margin=1cm]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\newcommand{\logLogSlopeTriangle}[9]
{
% #1. Relative offset in x direction.
% #2. Width in x direction, so xA-xB.
% #3. Relative offset in y direction.
% #4. Slope d(y)/d(log10(x)).
% #5. Plot options.
% #6. xmin.
% #7. xmax.
% #8. ymin.
% #9. ymax.
\pgfplotsextra
{
% Calculate auxilliary quantities.
\pgfmathsetmacro{\xA}{#6^(1-#1)*#7^#1}
\pgfmathsetmacro{\yA}{#8^(1-#3)*#9^#3}
\pgfmathsetmacro{\xB}{#6^(1-(#1-#2))*#7^(#1-#2)}
\pgfmathsetmacro{\yB}{\yA}
\pgfmathsetmacro{\xC}{\xA}
\pgfmathsetmacro{\yC}{\yA/(\xB^#4)*\xA^#4}
% Define coordinates for \draw.
\coordinate (A) at (axis cs:\xA,\yA);
\coordinate (B) at (axis cs:\xB,\yB);
\coordinate (C) at (axis cs:\xC,\yC);
% Draw slope triangle.
\draw[#5] (A)-- node[pos=0.5,anchor=north] {1}
(B)--
(C)-- node[pos=0.5,anchor=west] {#4}
cycle;
}
}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}
[
%xmin=10^1, % WHAT IF I DON'T USE THIS?
%xmax=10^4, % WHAT IF I DON'T USE THIS?
xlabel=$x$,
%ymin=10^0.5, % WHAT IF I DON'T USE THIS?
%ymax=10^4, % WHAT IF I DON'T USE THIS?
ylabel style={rotate=-90},
ylabel=$y$,
grid=major,
clip=false
]
\addplot[blue,line width=1pt,domain=10^1:10^4] {sqrt(x)};
\addplot[red,line width=1pt,domain=10^1:10^4] {x};
\pgfplotsextra
{
\pgfkeysgetvalue{/pgfplots/xmin}{\xmin}
\pgfkeysgetvalue{/pgfplots/xmax}{\xmax}
\pgfkeysgetvalue{/pgfplots/ymin}{\ymin}
\pgfkeysgetvalue{/pgfplots/ymax}{\ymax}
}
\logLogSlopeTriangle{0.9}{0.1}{0.1}{1/2}{blue}{\xmin}{\xmax}{\ymin}{\ymax};
\logLogSlopeTriangle{0.75}{0.1}{0.1}{1}{red}{\xmin}{\xmax}{\ymin}{\ymax};
\end{loglogaxis}
\end{tikzpicture}
\end{document}
results in 
. As you can see, the triangles are in the wrong place. How do I fix this?



\show\xminto see what they hold and compare it with the axis labels. – percusse May 17 '15 at 11:37\pgfplotsextraI need other values for the coordinates of triangles. Why does\pgfplotsextramodify this? Nonetheless, I tried to define the coordinates(A),(B)and(C)by(axis cs:10^\xA,10^\yA),(axis cs:10^\xB,10^\yB)and(axis cs:10^\xC,10^\yC)., but this still gives the wrong coordinates. Printing the values with and without\pgfplotsextra, I get for\xA8.01729000 · 10^0and5.01134879 · 10^3respectively. These numbers do not seem to be related by thelog10orlogfunction. I'm not sure how to proceed. – Adriaan May 17 '15 at 19:22(A),(B)and(C)by(axis cs:10^\xA,10^\yA),(axis cs:10^\xB,10^\yB)and(axis cs:10^\xC,10^\yC)still gives the wrong coordinates. I do not think the coordinates with and without\pgfplotsextraare related through thelogorlog10functions but by another function. I do not know which. What do you think? – Adriaan May 17 '15 at 19:26(axis cs:10^\xA,10^\yA), but replace\xminwith(exp(\xmin))instead, to get the proper coordinates(A),(B)and(C), see my answer below. – Adriaan May 18 '15 at 09:25