2

I created a diagram whose code I give below. I would like to be able to accumulate diagrams on the same graph with a parameter that changes: in my wish, it is to modify the concentration (currently, it is 0.1 but I would also like to add the diagram with 0.01 and possibly other values ). How can I do ?

\documentclass[border=0.2cm,11pt]{standalone}%conversion en png avec ghostscript

%COMPILé AVEC XeLaTeX

\usepackage[T1]{fontenc}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{lmodern}
\mathcode`\.="013B%virgule décimale en mode math
%\usepackage{tikz}% chargé automatiquement par pgfplots
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\pgfplotsset{/pgf/number format/.cd,1000 sep={~},use comma}
\usepackage{siunitx}% unités et écriture de valeurs
\usepackage[modules={all}]{chemmacros}%le module orbital a été commenté (lignes 3816 à 3824
\usetikzlibrary{spy}

\begin{document}

\def\concentration{0.1}
\def\pKe{14}
\def\pKsII{15.1}
\def\pKsIII{38}
\def\potstandFeIIIFeII{0.77}
\def\potstandFeIIFe{-0.44}

\pgfmathsetmacro{\phPrecipitationII}{\pKe-0.5*\pKsII-0.5*log10(\concentration)}
\pgfmathsetmacro{\phPrecipitationIII}{\pKe-1/3*\pKsIII-1/3*log10(\concentration)}


\pgfmathsetmacro{\potFeIIFe}{\potstandFeIIFe +0.03*log10(\concentration)}
\pgfmathsetmacro{\potFeIIIFeII}{\potstandFeIIIFeII}
\pgfmathsetmacro{\potstandFeOHIIIFeII}{\potstandFeIIIFeII+0.18*\phPrecipitationIII+0.06*log10(\concentration)}
\pgfmathsetmacro{\potstandFeOHIIIFeOHII}{\potstandFeOHIIIFeII-0.18*\phPrecipitationII-0.06*log10(\concentration)+0.06*\phPrecipitationII}
\pgfmathsetmacro{\potFeOHIIhaut}{\potstandFeOHIIIFeOHII-0.06*\phPrecipitationII}
\pgfmathsetmacro{\potstandFeOHIIFe}{\potFeIIFe+0.06*\phPrecipitationII}


\footnotesize
    \begin{tikzpicture}
    \begin{axis}[% mise en forme du graphique :
    xmin=0,xmax=14,
    ymin=-1,ymax=2,
    xlabel=pH,
    ylabel={$E (\unit{\V})$},
    ytick distance=0.5,
    minor y tick num=4,
    minor x tick num=4,
    grid style={black!10},
    grid=both,% grilles primaire et secondaire
    axis line shift=1pt,
    xtick align=outside,
    ytick align=outside,
    tickpos=left,
    legend pos=north west,% place de la légende,
    axis x line=bottom,
    axis y line = left,
    samples = 200,
    no markers,
    ]
    
    %frontiere horiz Fe/FeII
    \addplot expression[domain=0:\phPrecipitationII, red, solid, very thin]{\potFeIIFe}; 
        
    %frontiere horiz FeII/FeIII 
        \addplot expression[domain=0:\phPrecipitationIII, red, solid, very thin]{\potFeIIIFeII};
        
    %frontiere verticale FeII
        \addplot [red, very thin]coordinates{(\phPrecipitationII,\potFeIIFe) (\phPrecipitationII,\potFeOHIIhaut)}; 

    %frontiere vericale FeIII
        \addplot [red, very thin]coordinates{(\phPrecipitationIII,\potFeIIIFeII) (\phPrecipitationIII,2)}; 

    %frontiere oblique Fe(OH)3 et Fe2+
            \addplot expression[domain=\phPrecipitationII:\phPrecipitationIII, red, solid, very thin]{\potstandFeOHIIIFeII-0.18*x-0.06*log10(\concentration)};

    %frontiere oblique Fe(OH)3 et Fe(OH)2
            \addplot expression[domain=\phPrecipitationII:14, red, solid, very thin]{\potstandFeOHIIIFeOHII-0.06*x};

%frontiere oblique Fe(OH)2 et Fe
            \addplot expression[domain=\phPrecipitationII:14, red, solid, very thin]{\potstandFeOHIIFe-0.06*x};

        \node at (4,-0.75){\ch{Fe\sld}};
        \node at (4,0){\ch{Fe\pch[2]}};
        \node at (1,1.2){\ch{Fe\pch[3]}};
        \node at (8,0.6){\ch{Fe(OH)3\sld}};
        \node[rotate=-12] at (11,-0.6){\ch{Fe(OH)2\sld}};

%\spy on (6.82,3.65) in node at (8.5,3.8);
    \end{axis}
    \end{tikzpicture}
\end{document}

screenshot scrrenshot2

6
  • I can not compile your code. Undefined control sequence. \potFeOHIIhaut Commented Jul 25, 2024 at 21:50
  • @hpekristiansen oops, I forgot to paste a part: it's corrected Commented Jul 26, 2024 at 4:20
  • Can you please add a screenshot? Thank you Commented Jul 26, 2024 at 6:55
  • "I would also like to add the diagram with 0.01 and possibly other values" -- Do you want to superimpose one or more diagrams, with the same styles (red etc.) ? // BTW, your code also compiles with pdflatex ... Commented Jul 26, 2024 at 7:02
  • 1
    @MS-SPO I edited with 2 screenshots: the 1st with the value of 0.1; the second with 0.01 I want to overlay with a different style (it doesn't matter if it's via different colors or stroke shapes) Commented Jul 26, 2024 at 7:04

2 Answers 2

3

Here's one way to do it:

  • move the definitions inside a \foreach loop
  • see lines commented with % <<<

result

\documentclass[border=0.2cm,11pt]{standalone}%conversion en png avec ghostscript

%COMPILé AVEC XeLaTeX

\usepackage[T1]{fontenc}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{lmodern}
\mathcode`\.="013B%virgule décimale en mode math
%\usepackage{tikz}% chargé automatiquement par pgfplots
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\pgfplotsset{/pgf/number format/.cd,1000 sep={~},use comma}
\usepackage{siunitx}% unités et écriture de valeurs
\usepackage[modules={all}]{chemmacros}%le module orbital a été commenté (lignes 3816 à 3824
\usetikzlibrary{spy}

\begin{document}

\footnotesize
 \begin{tikzpicture}

    \begin{axis}[% mise en forme du graphique :
    xmin=0,xmax=14,
    ymin=-1,ymax=2,
    xlabel=pH,
    ylabel={$E (\unit{\V})$},
    ytick distance=0.5,
    minor y tick num=4,
    minor x tick num=4,
    grid style={black!10},
    grid=both,% grilles primaire et secondaire
    axis line shift=1pt,
    xtick align=outside,
    ytick align=outside,
    tickpos=left,
    legend pos=north west,% place de la légende,
    axis x line=bottom,
    axis y line = left,
    samples = 200,
    no markers,
    ]

\foreach \c in {0.1, 0.01, 0.001}{  % <<<

\def\concentration{\c}      % <<<
\def\pKe{14}
\def\pKsII{15.1}
\def\pKsIII{38}
\def\potstandFeIIIFeII{0.77}
\def\potstandFeIIFe{-0.44}

\pgfmathsetmacro{\phPrecipitationII}{\pKe-0.5*\pKsII-0.5*log10(\concentration)}
\pgfmathsetmacro{\phPrecipitationIII}{\pKe-1/3*\pKsIII-1/3*log10(\concentration)}


\pgfmathsetmacro{\potFeIIFe}{\potstandFeIIFe +0.03*log10(\concentration)}
\pgfmathsetmacro{\potFeIIIFeII}{\potstandFeIIIFeII}
\pgfmathsetmacro{\potstandFeOHIIIFeII}{\potstandFeIIIFeII+0.18*\phPrecipitationIII+0.06*log10(\concentration)}
\pgfmathsetmacro{\potstandFeOHIIIFeOHII}{\potstandFeOHIIIFeII-0.18*\phPrecipitationII-0.06*log10(\concentration)+0.06*\phPrecipitationII}
\pgfmathsetmacro{\potFeOHIIhaut}{\potstandFeOHIIIFeOHII-0.06*\phPrecipitationII}
\pgfmathsetmacro{\potstandFeOHIIFe}{\potFeIIFe+0.06*\phPrecipitationII} 

    
    %frontiere horiz Fe/FeII
    \addplot expression[domain=0:\phPrecipitationII, red, solid, very thin]{\potFeIIFe}; 
        
    %frontiere horiz FeII/FeIII 
        \addplot expression[domain=0:\phPrecipitationIII, red, solid, very thin]{\potFeIIIFeII};
        
    %frontiere verticale FeII
        \addplot [red, very thin]coordinates{(\phPrecipitationII,\potFeIIFe) (\phPrecipitationII,\potFeOHIIhaut)}; 

    %frontiere vericale FeIII
        \addplot [red, very thin]coordinates{(\phPrecipitationIII,\potFeIIIFeII) (\phPrecipitationIII,2)}; 

    %frontiere oblique Fe(OH)3 et Fe2+
            \addplot expression[domain=\phPrecipitationII:\phPrecipitationIII, red, solid, very thin]{\potstandFeOHIIIFeII-0.18*x-0.06*log10(\concentration)};

    %frontiere oblique Fe(OH)3 et Fe(OH)2
            \addplot expression[domain=\phPrecipitationII:14, red, solid, very thin]{\potstandFeOHIIIFeOHII-0.06*x};

%frontiere oblique Fe(OH)2 et Fe
            \addplot expression[domain=\phPrecipitationII:14, red, solid, very thin]{\potstandFeOHIIFe-0.06*x};

        \node at (4,-0.75){\ch{Fe\sld}};
        \node at (4,0){\ch{Fe\pch[2]}};
        \node at (1,1.2){\ch{Fe\pch[3]}};
        \node at (8,0.6){\ch{Fe(OH)3\sld}};
        \node[rotate=-12] at (11,-0.6){\ch{Fe(OH)2\sld}};

%\spy on (6.82,3.65) in node at (8.5,3.8);
 }  % end of foreach    <<<
    \end{axis}
 \end{tikzpicture}
\end{document}
5
  • BTW, for the nodes coordinates you should use axis cs: , see tikz.dev/pgfplots/reference-annotations#sec-4.17.1 . Makes life much easier ... Commented Jul 26, 2024 at 7:17
  • thanks ; is it possible to modify the color of the line for each loop? (to facilitate graphic reading) i don't understand what could be simpler with axis cs Commented Jul 26, 2024 at 7:23
  • axis cs: interpretes your coordinate in the units of your x or y axis, no longer in centimeters. Try with other ranges, order of magnitudes etc. Commented Jul 26, 2024 at 7:27
  • The usual way would be to introduce pairs like \foreach \c/\clr in {.1/blue} …, but it didn’t accept replacing red with \clr in the first \addplot. Perhaps a different name will do. Sometimes there seem to be cliffs like this when you „challenge“ the underlying TEX implementation of Tikz/pgfplots. There certainly is a way to overcome this … for the veterans here :) Commented Jul 26, 2024 at 7:32
  • 1
    ah ok; I think I'll put them side by side then. Thanks Commented Jul 26, 2024 at 7:46
1

A follow-up answer to the request for varying color.

This requires a slightly different approach, as it's well known, that \foreach doesn't allways work in pgfplots as we are used to. Basically proposals are:

  • use \pgfplotsinvokeforeach or \pgfplotsforeachungrouped, see here, there or here
  • use a \edef\temp{...}\temp approach, e.g. here

I go with the latter, reworking the other solution. The first part \edef\temp{...} defines and expands a command, while the second \temp just calls it.

In the code I once demonstrated the changed call, together with the color from the loop ... color=\xx, ...:

    % ~~~ recommended code ~~~~~~~~~~~~~~~~~~~
    %frontiere horiz Fe/FeII
    \edef\temp{
        \noexpand\addplot expression[domain=0:\phPrecipitationII, color=\xx, solid, very thin]{\potFeIIFe};
        }
    \temp

and for the rest used refactoring, i.e. introduced this new command:

...
% ~~~ from refactoring ~~~~~~~
\newcommand\loopclr[1]{\edef\temp{\noexpand#1}\temp }
...
    % ~~~ using refactoring, i.e. the new command ~~~~~~~~~~~~~~~~~~
    %frontiere horiz FeII/FeIII     
    \loopclr{\addplot expression[domain=0:\phPrecipitationIII, color=\xx, solid, very thin]{\potFeIIIFeII};}

Though it works, I'm pretty sure it's not an optimal approach.

result

\documentclass[border=0.2cm,11pt]{standalone}%conversion en png avec ghostscript

%COMPILé AVEC XeLaTeX

\usepackage[T1]{fontenc}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{lmodern}
\mathcode`\.="013B%virgule décimale en mode math
%\usepackage{tikz}% chargé automatiquement par pgfplots
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\pgfplotsset{/pgf/number format/.cd,1000 sep={~},use comma}
\usepackage{siunitx}% unités et écriture de valeurs
\usepackage[modules={all}]{chemmacros}%le module orbital a été commenté (lignes 3816 à 3824
\usetikzlibrary{spy}

% ~~~ from refactoring ~~~~~~~
\newcommand\loopclr[1]{\edef\temp{\noexpand#1}\temp }

% ~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}

\footnotesize
 \begin{tikzpicture}

    \begin{axis}[% mise en forme du graphique :
    xmin=0,xmax=14,
    ymin=-1,ymax=2,
    xlabel=pH,
    ylabel={$E (\unit{\V})$},
    ytick distance=0.5,
    minor y tick num=4,
    minor x tick num=4,
    grid style={black!10},
    grid=both,% grilles primaire et secondaire
    axis line shift=1pt,
    xtick align=outside,
    ytick align=outside,
    tickpos=left,
    legend pos=north west,% place de la légende,
    axis x line=bottom,
    axis y line = left,
    samples = 200,
    no markers,
    ]
    

\foreach \c / \xx in {0.1/red, 0.01/blue, 0.001/cyan}{  % <<<

\def\concentration{\c}      % <<<
\def\pKe{14}
\def\pKsII{15.1}
\def\pKsIII{38}
\def\potstandFeIIIFeII{0.77}
\def\potstandFeIIFe{-0.44}

\pgfmathsetmacro{\phPrecipitationII}{\pKe-0.5*\pKsII-0.5*log10(\concentration)}
\pgfmathsetmacro{\phPrecipitationIII}{\pKe-1/3*\pKsIII-1/3*log10(\concentration)}


\pgfmathsetmacro{\potFeIIFe}{\potstandFeIIFe +0.03*log10(\concentration)}
\pgfmathsetmacro{\potFeIIIFeII}{\potstandFeIIIFeII}
\pgfmathsetmacro{\potstandFeOHIIIFeII}{\potstandFeIIIFeII+0.18*\phPrecipitationIII+0.06*log10(\concentration)}
\pgfmathsetmacro{\potstandFeOHIIIFeOHII}{\potstandFeOHIIIFeII-0.18*\phPrecipitationII-0.06*log10(\concentration)+0.06*\phPrecipitationII}
\pgfmathsetmacro{\potFeOHIIhaut}{\potstandFeOHIIIFeOHII-0.06*\phPrecipitationII}
\pgfmathsetmacro{\potstandFeOHIIFe}{\potFeIIFe+0.06*\phPrecipitationII} 

    % ~~~ recommended code ~~~~~~~~~~~~~~~~~~~
    %frontiere horiz Fe/FeII
    \edef\temp{
        \noexpand\addplot expression[domain=0:\phPrecipitationII, color=\xx, solid, very thin]{\potFeIIFe};
        }
    \temp
    
    % ~~~ using refactoring, i.e. the new command ~~~~~~~~~~~~~~~~~~
    %frontiere horiz FeII/FeIII     
    \loopclr{\addplot expression[domain=0:\phPrecipitationIII, color=\xx, solid, very thin]{\potFeIIIFeII};}
      
    
    %frontiere verticale FeII
    \loopclr{\addplot [color=\xx, very thin]coordinates{(\phPrecipitationII,\potFeIIFe) (\phPrecipitationII,\potFeOHIIhaut)};} 


    %frontiere vericale FeIII
    \loopclr{\addplot [color=\xx, very thin]coordinates{(\phPrecipitationIII,\potFeIIIFeII) (\phPrecipitationIII,2)}; }


    %frontiere oblique Fe(OH)3 et Fe2+
    \loopclr{\addplot expression[domain=\phPrecipitationII:\phPrecipitationIII, color=\xx, solid, very thin]{\potstandFeOHIIIFeII-0.18*x-0.06*log10(\concentration)};}


    %frontiere oblique Fe(OH)3 et Fe(OH)2
    \loopclr{\addplot expression[domain=\phPrecipitationII:14, color=\xx, solid, very thin]{\potstandFeOHIIIFeOHII-0.06*x};}

    %frontiere oblique Fe(OH)2 et Fe
    \loopclr{\addplot expression[domain=\phPrecipitationII:14, color=\xx, solid, very thin]{\potstandFeOHIIFe-0.06*x};}

        \node at (4,-0.75){\ch{Fe\sld}};
        \node at (4,0){\ch{Fe\pch[2]}};
        \node at (1,1.2){\ch{Fe\pch[3]}};
        \node at (8,0.6){\ch{Fe(OH)3\sld}};
        \node[rotate=-12] at (11,-0.6){\ch{Fe(OH)2\sld}};

%\spy on (6.82,3.65) in node at (8.5,3.8);
 }  % end of foreach    <<<
    \end{axis}
 \end{tikzpicture}
\end{document}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.