pgfplots: Como definir variáveis ​​com números retirados de um arquivo?

pgfplots: Como definir variáveis ​​com números retirados de um arquivo?

Enquanto trabalhava em um tópico científico, me perguntei se o seguinte seria possível: Quero traçar várias curvas exponenciais usando o \addplotcomando. A especialidade reside no fato de que preciso que os expoentes não sejam codificados, mas lidos em um arquivo de texto! Como isso poderia ser feito? Criei um MWE para esclarecer o que quero:

%% 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 você pode ver, os expoentes agora estão codificados (linhas 65-71), mas devem ser variáveis ​​flutuantes, retiradas do arquivo csv (que é gerado previamente por outro programa)! Eu me atrapalhei um pouco com o grandedatatoolpacote, mas não tenho certeza de como ou mesmo se é possível empregá-lo para minhas necessidades. O \DTLdisplaydb{LsqExponents}comando falha, por isso está comentado no MWE.

Então acho que a solução consiste em duas etapas:

  1. Obtendo os três números como variáveis ​​individuais em LaTeX/Tikz.
  2. Substituindo os expoentes codificados por essas variáveis.

Qualquer ajuda é apreciada!

Responder1

Aqui faço uma implementação usando meu readarraypacote, que lê dados separados por espaço. As linhas principais são

\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}

O que isso faz é ler o conteúdo do arquivo em um \defarquivo \mydatadef. Em seguida, leio \mydatadefe classifico-o em uma estrutura de array 2-D nomeada mydatacom largura de coluna = 1 (portanto, um array 1-D efetivamente). Os valores dos dados são acessados ​​com\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}

insira a descrição da imagem aqui

informação relacionada