pgfplots: ¿Cómo definir variables con números tomados de un archivo?

pgfplots: ¿Cómo definir variables con números tomados de un archivo?

Mientras trabajaba en un tema científico, me preguntaba si es posible lo siguiente: quiero trazar varias curvas exponenciales usando el \addplotcomando. La especialidad radica en el hecho de que necesito que los exponentes no estén codificados, ¡sino que se lean desde un archivo de texto! ¿Cómo podría lograrse esto? He creado un MWE para aclarar lo que quiero:

%% MWE for SX: Defining variables with numbers taken from a file
\documentclass{standalone}

\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
%% Use only sans-serif fonts; change to serif if desired
\renewcommand*\sfdefault{phv}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{arevmath}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary{spy, backgrounds}
\usepackage{pgfplotstable}
\usepackage{xcolor}
\usepackage{amsmath}
%% To read variables from file
\usepackage{datatool}
\usepackage{filecontents}

\newlength\figurewidth
\newlength\figureheight
\newlength\marksize

\begin{document}
%% Width and height of the output figure, adapt as necessary
\setlength{\figurewidth}{13cm}
\setlength{\figureheight}{8cm}
\setlength{\marksize}{2.4pt}
\setlength{\linewidth}{1pt}

%% Define the file in here, as whole files cannot be uploaded to Tex.SE
\begin{filecontents}{LsqExponents.csv}
4.012,3.456,2.345
\end{filecontents}
%% Now read these three values from the file
\DTLloaddb[noheader, keys={b_annu, b_rest, b_stra}]{LsqExponents}{LsqExponents.csv}
%\DTLdisplaydb{LsqExponents}

\begin{tikzpicture}[font=\large]

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0.05,
xmax=0.5,
xtick={0.1,0.2,0.3,0.4,0.5},
xlabel={$\text{T G S } \gamma_\mathrm{g} \text{ (-)}$},
%xmajorgrids,
ymin=0,
ymax=0.12,
ylabel={$\text{G R P } h_\mathrm{g} \text{ (-)}$},
yticklabel style={/pgf/number format/fixed,
/pgf/number format/precision=2,
/pgf/number format/fixed zerofill},
%ymajorgrids,
name=plot1,
legend pos=north west,
legend style={anchor=north west,draw=black,fill=white,legend cell align=left, rounded corners=2pt, nodes={inner sep=4pt,text depth=0pt}}
]
%% Fits and functions
\addplot [color=cyan, solid, domain=0:0.5] {x^4.567};
\addlegendentry{Original TD Fit};

\addplot [color=magenta, dashed, domain=0:0.5] {x^3.456};
\addlegendentry{TD-like Fit for Rest};

\addplot [color=black, dashed, domain=0:0.5] {x^2.345};
\addlegendentry{TD-like Fit for Stra};

\end{axis}

\end{tikzpicture}

\end{document}

Como puede ver, los exponentes ahora están codificados (líneas 65-71), pero deberían ser variables flotantes, tomadas del archivo csv (que es generado previamente por otro programa). Tropecé un poco con el grandatatoolpaquete, pero no estoy seguro de cómo o incluso si es posible emplearlo para mis necesidades. El \DTLdisplaydb{LsqExponents}comando falla, por eso está comentado en el MWE.

Entonces supongo que la solución consta de dos pasos:

  1. Obteniendo los tres números como variables individuales en LaTeX/Tikz.
  2. Reemplazar los exponentes codificados con estas variables.

¡Cualquier ayuda es apreciada!

Respuesta1

Aquí hago una implementación usando mi readarraypaquete, que lee datos separados por espacios. Las líneas clave son

\begin{filecontents*}{LsqExponents.ssv}
4.012 3.456 2.345
\end{filecontents*}
%% Now read these three values from the file
\readdef{LsqExponents.ssv}{\mydatadef}
\readArrayij{\mydatadef}{mydata}{1}

Lo que esto hace es leer el contenido del archivo en un \defarchivo \mydatadef. Luego, lo leo \mydatadefy lo clasifico en una estructura de matriz 2-D nombrada mydatacon ancho de columna = 1 (por lo tanto, una matriz 1-D efectivamente). Se accede a los valores de los datos con\arrayij{mydata}{<row value>}{1}

%% MWE for SX: Defining variables with numbers taken from a file
\documentclass{standalone}

\usepackage[utf8]{inputenx}
\usepackage[T1]{fontenc}
%% Use only sans-serif fonts; change to serif if desired
\renewcommand*\sfdefault{phv}
\renewcommand*{\familydefault}{\sfdefault}
\usepackage{arevmath}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}
\usetikzlibrary{spy, backgrounds}
\usepackage{pgfplotstable}
\usepackage{xcolor}
\usepackage{amsmath}
%% To read variables from file
\usepackage{readarray}
\usepackage{filecontents}

\newlength\figurewidth
\newlength\figureheight
\newlength\marksize

\begin{document}
%% Width and height of the output figure, adapt as necessary
\setlength{\figurewidth}{13cm}
\setlength{\figureheight}{8cm}
\setlength{\marksize}{2.4pt}
\setlength{\linewidth}{1pt}

%% Define the file in here, as whole files cannot be uploaded to Tex.SE
\begin{filecontents*}{LsqExponents.ssv}
4.012 3.456 2.345
\end{filecontents*}
%% Now read these three values from the file
\readdef{LsqExponents.ssv}{\mydatadef}
\readArrayij{\mydatadef}{mydata}{1}

\begin{tikzpicture}[font=\large]

\begin{axis}[%
width=\figurewidth,
height=\figureheight,
scale only axis,
xmin=0.05,
xmax=0.5,
xtick={0.1,0.2,0.3,0.4,0.5},
xlabel={$\text{T G S } \gamma_\mathrm{g} \text{ (-)}$},
%xmajorgrids,
ymin=0,
ymax=0.12,
ylabel={$\text{G R P } h_\mathrm{g} \text{ (-)}$},
yticklabel style={/pgf/number format/fixed,
/pgf/number format/precision=2,
/pgf/number format/fixed zerofill},
%ymajorgrids,
name=plot1,
legend pos=north west,
legend style={anchor=north west,draw=black,fill=white,legend cell align=left, rounded corners=2pt, nodes={inner sep=4pt,text depth=0pt}}
]
%% Fits and functions
\addplot [color=cyan, solid, domain=0:0.5] {x^\arrayij{mydata}{1}{1}};
\addlegendentry{Original TD Fit};

\addplot [color=magenta, dashed, domain=0:0.5] {x^\arrayij{mydata}{2}{1}};
\addlegendentry{TD-like Fit for Rest};

\addplot [color=black, dashed, domain=0:0.5] {x^\arrayij{mydata}{3}{1}};
\addlegendentry{TD-like Fit for Stra};

\end{axis}

\end{tikzpicture}

\end{document}

ingrese la descripción de la imagen aquí

información relacionada