6

I am using latex to generate figures, here is a simple example.

\documentclass[
border={0mm 0mm 0mm 0mm}, % left bottom right top
]{standalone}
\usepackage{tikz,stackengine}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}

\begin{document}
    
    \pgfplotsset{width=5cm,height=5cm,grid style={dashed}}
    \begin{filecontents}{curves.txt}
        x  y1 y2
        1  1  2
        2  4  5
        3  2  3
        4  6  7
        5  5  6
        6  6  7
        7  9  5
        8  5  8
    \end{filecontents}
    % command in the matlab
    % [Time_Series_Plotted',p_total_from_TU(:,Time_Series_Plotted)',p_total_from_TU_CHP(:,Time_Series_Plotted)',p_total_from_TU_CHP_WF(:,Time_Series_Plotted)',p_total_of_loads(:,Time_Series_Plotted)',p_total_from_TU_CHP_WF_WCur(:,Time_Series_Plotted)']
    \pgfplotstableread[skip first n=5,]{curves.txt}{\curves}% 2+4 for filecontents header
        \begin{tikzpicture}
            \begin{axis}
                [color=black,grid=major,use units, xlabel=x, ylabel=y,
                legend style={draw=none,fill=none,legend image post style={scale=1}, font=\scriptsize}, legend cell align={left}, legend pos=north west,]
                \addplot[dashed,draw=green, line width=1] table [x index=0,y index=1, ]{\curves};
                \addplot[       draw=green, line width=1] table [x index=0,y index=2, ]{\curves};
                \legend{curve1, curve2};
            \end{axis}
        \end{tikzpicture}
\end{document}

I use \begin{filecontents} and \end{filecontents} to create the .txt file to store the data for tikzpicture. Then, after compiling the latex code, I have a curves.txt file in the folder.

However, when adjust parameters of curves in the latex file. I should delete the curves.txt file firstly, and then compile the latex code. If not, the latex will still draw curves using old values of curves.txt.

Hence, are there some methods to delete the curves.txt file after generate the pdf file, or cover the old curves.txt file each time when complie the latex code, or just let pgfplots read data from the latex code without reading from files?

1
  • 3
    your title asks about deleting aux and log but your question is about completely different files, please edit the title or question to match each other Commented Aug 3, 2023 at 9:19

2 Answers 2

4

You don't need an external file and adjusting the values in the code below is just as easy as with filecontents.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}

\begin{document}
    
\begin{tikzpicture}
\pgfplotsset{width=5cm,height=5cm,grid style={dashed}}
\pgfplotstableread{
  x  y1 y2
  1  1  2
  2  4  5
  3  2  3
  4  6  7
  5  5  6
  6  6  7
  7  9  5
  8  5  8
}{\curves}
\begin{axis}[
  color=black,
  grid=major,
  use units,
  xlabel=x,
  ylabel=y,
  legend style={
    draw=none,
    fill=none,
    legend image post style={scale=1},
    font=\scriptsize,
  },
  legend cell align=left,
  legend pos=north west,
]
\addplot[dashed, draw=green, line width=1] table [x index=0,y index=1, ]{\curves};
\addplot[        draw=green, line width=1] table [x index=0,y index=2, ]{\curves};
\legend{curve1, curve2}
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

2
  • Seconds earlier. ;-)
    – cabohah
    Commented Aug 3, 2023 at 9:38
  • Not even a full minute! Sorry! 😊
    – egreg
    Commented Aug 3, 2023 at 10:03
6

You can use \begin{filecontents*}[overwrite]{curves.txt}...\end{filecontents*} to ensure that your .txt file is recreated:

\documentclass[
border={0mm 0mm 0mm 0mm}, % left bottom right top
]{standalone}
\usepackage{tikz,stackengine}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{units}

\begin{document}
    
    \pgfplotsset{width=5cm,height=5cm,grid style={dashed}}
    \begin{filecontents*}[overwrite]{curves.txt}
        x  y1 y2
        1  1  2
        2  4  5
        3  2  3
        4  6  7
        5  5  6
        6  6  7
        7  9  5
        8  5  8
    \end{filecontents*}
    % command in the matlab
    % [Time_Series_Plotted',p_total_from_TU(:,Time_Series_Plotted)',p_total_from_TU_CHP(:,Time_Series_Plotted)',p_total_from_TU_CHP_WF(:,Time_Series_Plotted)',p_total_of_loads(:,Time_Series_Plotted)',p_total_from_TU_CHP_WF_WCur(:,Time_Series_Plotted)']
    \pgfplotstableread[skip first n=5,]{curves.txt}{\curves}% 2+4 for filecontents header
        \begin{tikzpicture}
            \begin{axis}
                [color=black,grid=major,use units, xlabel=x, ylabel=y,
                legend style={draw=none,fill=none,legend image post style={scale=1}, font=\scriptsize}, legend cell align={left}, legend pos=north west,]
                \addplot[dashed,draw=green, line width=1] table [x index=0,y index=1, ]{\curves};
                \addplot[       draw=green, line width=1] table [x index=0,y index=2, ]{\curves};
                \legend{curve1, curve2};
            \end{axis}
        \end{tikzpicture}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .