不能在 pgfplots 中的绘图域中包含全局变量

不能在 pgfplots 中的绘图域中包含全局变量

考虑将以下一段代码包含在包\begin{axis} ... \end{axis}pgfplots

    \newcommand{\lineasVolumen}[3]{%

      % #1,2 Volumen inicial, volumen final, #3 paso.

      % Líneas de volumen constante:
      \FPeval{\segundo}{(#1) + (#3)}

      \pgfplotsforeachungrouped \volumen in {#1,\segundo,...,#2}{%

      \addplot[white,samples=201,name path global=lineaVolumen,
                                 domain=\minTs:\maxTs]
        {621.98*(((\Pt)*(\volumen))/(287 *(x+273.159)) - 1)};
      \path [name intersections={of=lineaSaturacion and lineaVolumen,
                           by=E}];

      % Para dibujar las coordenadas x,y de la intersección:
      \node[anchor=south, fill=white, fill opacity=0.75,text opacity=1] at (E) {
       \pgfgetlastxy{\macrox}{\macroy}
       \transformxdimension{\macrox}
       \pgfmathprintnumber{\pgfmathresult},%
       \transformydimension{\macroy}%
       \pgfmathprintnumber{\pgfmathresult} 
    };

    \node at (E) { 
      \pgfgetlastxy{\macrox}{\macroy}
      \transformxdimension{\macrox}
      \global\edef\coordX{\pgfmathresult}
    };

    \node at (E) {\coordX}; % HERE WORKS FINE.

    \addplot[purple,samples=201,domain=\coordX:\maxTs] % HERE IT'S WRONG.
        {621.98*(((\Pt)*(\volumen))/(287 *(x+273.159)) - 1)};    
   }

 }

当我运行 pdflatex 时出现以下错误:

! 未定义控制序列。\pgfplots@plot@domain ->\coordX :\maxTs

l.259 \lineasVolumen{0.75}{0.95}{0.05}

我很沮丧,因为我尝试通过互联网寻找解决方案但没有成功。

我们需要保存交叉点的 x 坐标,然后在的域中使用\addplot

我需要帮助!提前致谢!

在此处输入图片描述

这里是代码:

    \documentclass{article}

    \usepackage[a4paper,landscape,
                left=1.5cm,right=1cm,
                top=1.5cm,bottom=1.5cm,
        headsep=0cm,footskip=0cm]
               {geometry}

    \usepackage{pxfonts} % palatino con símbolos matemáticos (incompatible amsmath)           

    \usepackage[spanish,es-nodecimaldot]{babel}
    \renewcommand\shorthandsspanish{} % evita conflictos con tikz (ver spanish.pdf y
                              % notas.txt)
    \spanishdecimal{,} %\decimalpoint

    \usepackage{tikz}
    \usepackage{pgfplots}
    %\pgfplotsset{compat=1.5}
    \usetikzlibrary{calc,intersections,through}
    \usepgfplotslibrary{fillbetween}
    \usepackage{fp}
    \usepackage{xifthen}

    \makeatletter
    \newcommand\transformxdimension[1]{
        \pgfmathparse{((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/10^\pgfplots@data@scale@trafo@EXPONENT@x}
    }
    \newcommand\transformydimension[1]{
        \pgfmathparse{((#1/\pgfplots@y@veclength)+\pgfplots@data@scale@trafo@SHIFT@y)/10^\pgfplots@data@scale@trafo@EXPONENT@y}
    }
    \makeatother

    % PARÁMETROS DEL DIAGRAMA ******************************************************

    \newcommand{\ancho}{192mm}
    \newcommand{\alto}{122mm}

    \newcommand{\Pt}{101325} % en pascales (101325 a nivel del mar)

    \newcommand{\minTs}{-10} \newcommand{\maxTs}{55}
    \newcommand{\pasoTsPral}{5} \newcommand{\pasoTsSec}{1}  % para rejilla de Ts

    \newcommand{\minW}{0} \newcommand{\maxW}{33}

    \newcommand{\Pvs}{exp((16.461*x-114.86)/(x+231.67))}

    \newcommand{\lineasVolumen}[3]{%
      % #1,2 Volumen inicial, volumen final, #3 paso.
      % Líneas de volumen constante:
      \FPeval{\segundo}{(#1) + (#3)}

      \pgfplotsforeachungrouped \volumen in {#1,\segundo,...,#2}{%

        \addplot[white,samples=201,name path global=lineaVolumen,domain=\minTs:\maxTs]
                {621.98*(((\Pt)*(\volumen))/(287 *(x+273.159)) - 1)};
        \path [name intersections={of=lineaSaturacion and lineaVolumen,by=E}];

        % Para dibujar las coordenadas x,y de la intersección:
        \node[anchor=south, fill=white, fill opacity=0.75,text opacity=1] at (E) {
          \pgfgetlastxy{\macrox}{\macroy}
          \transformxdimension{\macrox}
          \pgfmathprintnumber{\pgfmathresult},%
          \transformydimension{\macroy}%
          \pgfmathprintnumber{\pgfmathresult} 
        };

        \node at (E) { % sólo aquí funciona, pero \x sólo vale en otro entrecorchete
          \pgfgetlastxy{\macrox}{\macroy}
          \transformxdimension{\macrox}
          \pgfmathsetmacro{\xx}{\pgfmathresult}
          \global\edef\coordX{\xx}
        };

        \node at (E) {\coordX};

        \addplot[purple,samples=201,domain=\coordX:\maxTs]
                {621.98*(((\Pt)*(\volumen))/(287 *(x+273.159)) - 1)};    
      }

    }

    % ******************************************************************************

    \pagestyle{empty}
    \begin{document}

    \vspace*{\fill}
    \begin{center} 
    \begin{tikzpicture}
      \begin{axis}[width=1.3*\ancho,height=1.3*\alto,
                   domain=\minTs:\maxTs,domain y=\minW:\maxW,
                   xmax=\maxTs,ymin=\minW,ymax=\maxW,
                   title={},
                   axis x line=bottom,xtick={\minTs,-\pasoTsPral,...,\maxTs},
                   axis y line=right,ytick={\minW,1,...,\maxW},yticklabel pos=right,
                   xlabel={},
                   ylabel={}]
      % curva de saturación:           
      \addplot[blue,samples=201,name path global=lineaSaturacion]
              {1000 / ((\Pt/1000)/(0.62198*1*\Pvs) -1.60777)};
      %
      \lineasVolumen{0.75}{0.95}{0.05} 
      \end{axis}
    \end{tikzpicture}
    \end{center}
    \vspace*{\fill}


    \end{document}

答案1

该解决方案使用\pgfplotsextra\path代替。\draw\addplot

\documentclass{article}

    \usepackage[a4paper,landscape,
                left=1.5cm,right=1cm,
                top=1.5cm,bottom=1.5cm,
        headsep=0cm,footskip=0cm]
               {geometry}

    \usepackage{pxfonts} % palatino con símbolos matemáticos (incompatible amsmath)           

    \usepackage[spanish,es-nodecimaldot]{babel}
    \renewcommand\shorthandsspanish{} % evita conflictos con tikz (ver spanish.pdf y
                              % notas.txt)
    \spanishdecimal{,} %\decimalpoint

    \usepackage{tikz}
    \usepackage{pgfplots}
    %\pgfplotsset{compat=1.5}
    \usetikzlibrary{calc,intersections,through}
    \usepgfplotslibrary{fillbetween}
    \usepackage{fp}
    \usepackage{xifthen}

    \newcounter{id}

    \makeatletter
    \newcommand\transformxdimension[1]{
        \pgfmathparse{((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/10^\pgfplots@data@scale@trafo@EXPONENT@x}
    }
    \newcommand\transformydimension[1]{
        \pgfmathparse{((#1/\pgfplots@y@veclength)+\pgfplots@data@scale@trafo@SHIFT@y)/10^\pgfplots@data@scale@trafo@EXPONENT@y}
    }
    \makeatother

    % PARÁMETROS DEL DIAGRAMA ******************************************************

    \newcommand{\ancho}{192mm}
    \newcommand{\alto}{122mm}

    \newcommand{\Pt}{101325} % en pascales (101325 a nivel del mar)

    \newcommand{\minTs}{-10} \newcommand{\maxTs}{55}
    \newcommand{\pasoTsPral}{5} \newcommand{\pasoTsSec}{1}  % para rejilla de Ts

    \newcommand{\minW}{0} \newcommand{\maxW}{33}

    \newcommand{\Pvs}{exp((16.461*x-114.86)/(x+231.67))}

    \newcommand{\lineasVolumen}[3]{%
      % #1,2 Volumen inicial, volumen final, #3 paso.
      % Líneas de volumen constante:
      \FPeval{\segundo}{(#1) + (#3)}

      \foreach \volumen in {#1,\segundo,...,#2}{%
        \path[samples=201,domain=\minTs:\maxTs,name path global=lineaVolumen]
          plot (axis cs: \x, {621.98*(((\Pt)*(\volumen))/(287 *(\x+273.159)) - 1)});
        \path [name intersections={of=lineaSaturacion and lineaVolumen,by=E}];
        % Para dibujar las coordenadas x,y de la intersección:
        \node[anchor=south, fill=white, fill opacity=0.75,text opacity=1] at (E) {
          \pgfgetlastxy{\macrox}{\macroy}
          \transformxdimension{\macrox}
          \pgfmathprintnumber{\pgfmathresult},%
          \transformydimension{\macroy}%
          \pgfmathprintnumber{\pgfmathresult} 
        };
        \node at (E) { % sólo aquí funciona, pero \x sólo vale en otro entrecorchete
          \pgfgetlastxy{\macrox}{\macroy}
          \transformxdimension{\macrox}
          \pgfmathsetmacro{\xx}{\pgfmathresult}
          \global\edef\coordX{\xx}
        };
        \node at (E) {\coordX};
        \draw[red,samples=201,domain=\coordX:\maxTs,name path global=lineaVolumen]
          plot (axis cs: \x, {621.98*(((\Pt)*(\volumen))/(287 *(\x+273.159)) - 1)});
      };
    }

    % ******************************************************************************

    \pagestyle{empty}
    \begin{document}

    \vspace*{\fill}
    \begin{center} 
    \begin{tikzpicture}
      \begin{axis}[width=1.3*\ancho,height=1.3*\alto,
                   domain=\minTs:\maxTs,domain y=\minW:\maxW,
                   xmin=\minTs,xmax=\maxTs,ymin=\minW,ymax=\maxW,
                   title={},
                   axis x line=bottom,xtick={\minTs,-\pasoTsPral,...,\maxTs},
                   axis y line=right,ytick={\minW,1,...,\maxW},yticklabel pos=right,
                   xlabel={},
                   ylabel={}]
      % curva de saturación:           
      \addplot[blue,samples=201,name path global=lineaSaturacion]
              {1000 / ((\Pt/1000)/(0.62198*1*\Pvs) -1.60777)};
      %
      \pgfplotsextra{\lineasVolumen{0.75}{0.95}{0.05}}
      \end{axis}
    \end{tikzpicture}
    \end{center}
    \vspace*{\fill}

\end{document}

演示

相关内容