pgfplots:如何使用從文件中取得的數字定義變數?

pgfplots:如何使用從文件中取得的數字定義變數?

在研究科學主題時,我想知道以下是否可能:我想使用該\addplot命令繪製幾條指數曲線。特殊之處在於我不需要對指數進行硬編碼,而是從文字檔案中讀取!這怎麼可能實現呢?我創建了一個 MWE 來澄清我想要的:

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

正如您所看到的,指數現在是硬編碼的(第 65-71 行),但應該是浮點變量,取自 csv 檔案(由另一個程式預先生成)!我對偉大的事情有點摸索datatool包,但我不確定如何或是否可以使用它來滿足我的需求。該\DTLdisplaydb{LsqExponents}命令失敗,這就是它在 MWE 中被註解掉的原因。

所以我想解決方案包括兩個步驟:

  1. 在 LaTeX/Tikz 中取得三個數字作為單獨的變數。
  2. 用這些變數取代硬編碼指數。

任何幫助表示讚賞!

答案1

在這裡,我使用我的包進行了一個實現readarray,它讀取空格分隔的資料。關鍵行是

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

其作用是將文件內容讀入\def命名的\mydatadef.然後,我讀取\mydatadef並將其排序到以列寬 = 1 命名的二維數組結構中mydata(因此,實際上是一個一維數組)。數據值透過以下方式訪問\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}

在此輸入影像描述

相關內容