为什么将 tikz 文件的内容粘贴到我的主文件中可以正确编译,但使用 \input 时却不行

为什么将 tikz 文件的内容粘贴到我的主文件中可以正确编译,但使用 \input 时却不行

如果我将图片内容放在tikz我的 main.tex 文件中,它就会像使用

\usepgfplotslibrary{external}
    \tikzexternalize 

随后:pdflatex --enable-shell

  \documentclass{article}
    \usepackage{tikz,pgfplots}
    \usepackage{subfig}
    \usepackage{tabularx}
    \pgfplotsset{compat=newest}
       \usepgfplotslibrary{external}
    \tikzexternalize
    \begin{document}
    \definecolor{mycolor1}{rgb}{0,0.75,0.75}

    \begin{tikzpicture}

    \begin{axis}[%
    width=4.52083333333333in,
    height=3.565625in,
    scale only axis,
    xmin=0, xmax=6,
    xlabel={cfu (Y)},
    ymin=0, ymax=1,
    ylabel={Probability density},
    legend style={draw=black,fill=white,legend cell align=left}
    ]
    \addplot [
    color=blue,
    solid,
    line width=1.5pt
    ]
    table{
    0.164920894751145 0.145245471693298
    0.34898483521757 0.314105513485571
    0.400206868657802 0.375704063846473
    0.431202365194672 0.415565960090126
    0.468283168514601 0.465368046385591
    0.468819173063357 0.466102333522882
    0.541086963948335 0.567617590141928
    0.558035142464694 0.591815958738304
    0.558424656178589 0.592372256340699
    0.567763246195775 0.605704754250354
    0.588442297122976 0.635140815037755
    0.588968308388865 0.635887013738116
    0.589496900268574 0.636636709024852
    0.615575125108802 0.673361419528354
    0.656478830152384 0.729356371745445
    0.663734249176834 0.739001710618186
    0.681134429788414 0.761691427683771
    0.699206339854542 0.784509230520803
    0.703174714510729 0.789406773282464
    0.710293205919594 0.798082837552696
    0.738324318268112 0.830765143080553
    0.743729355370618 0.836772857231647
    0.7628624687978 0.857202030543563
    0.789650071275085 0.883431847212714
    0.848769035460717 0.93014828764704
    0.857054257851899 0.935346913150545
    0.867490832222506 0.941393771927579
    0.892221633180695 0.953428704494102
    0.910732799951076 0.96026629015755
    0.942439202692875 0.967534739622871
    0.964173551423469 0.969220797343017
    0.966490207360103 0.969241291138126
    0.972340307586979 0.969156381555276
    0.999815929196087 0.966143308438322
    1.01840762385923 0.96167588822653
    1.02422624912665 0.959879519150392
    1.04820841787849 0.950503937405069
    1.05040526585597 0.949488520832702
    1.05188064663965 0.948792015052305
    1.06421741151945 0.942513866116551
    1.06952744475091 0.939564533487357
    1.07377729763307 0.937098333090976
    1.10837980144772 0.913625640841771
    1.12655613304724 0.898983298828636
    1.13577066384758 0.89098627132941
    1.1580472276478 0.870142330490693
    1.15859589504013 0.869603007065865
    1.18051156035791 0.847100007296536
    1.21401730562959 0.809369022407148
    1.33027028384446 0.656408242977467
    1.33483660875034 0.649970375448687
    1.37629546726956 0.590995765951455
    1.39873034424263 0.558976024508442
    1.42011477374599 0.528607866819228
    1.49811243643743 0.421418636969251
    1.55782976646071 0.345795703108512
    1.60683136399288 0.289407680329164
    1.67872959183145 0.217237684951087
    1.70586524394617 0.193407783948843
    2.09859653835273 0.0221208957939459
    2.23050096301146 0.0087062390469457
    };
    \addlegendentry{n=5};

    \addplot [
    color=green!50!black,
    solid,
    line width=1.5pt
    ]
    table{
    1.11061716777245 0.25439255248339
    1.32152703852018 0.444545345197107
    1.33743602693433 0.460242989969573
    1.42547263125263 0.547313752669856
    1.50599624519356 0.623697850962722
    1.5261804928616 0.641779387498937
    1.573838875732 0.682042674974274
    1.57730219814673 0.684816482012706
    1.62699159305991 0.721956657982538
    1.63486458952984 0.727347067179288
    1.68630344515834 0.758807092685913
    1.71784982886711 0.774600265244482
    1.83289913575602 0.806644546215021
    1.86864417529802 0.807887335418516
    1.88963804306077 0.806645110709114
    1.90172633188334 0.805269969440086
    1.97361138903204 0.787332803216098
    2.16847522483986 0.665794197723055
    2.26107642525521 0.582137894844234
    2.44093902647085 0.40561167899897
    2.55728899719626 0.299162501222641
    2.72596462417501 0.174351769994033
    3.14460320890567 0.0275718440302502
    };
    \addlegendentry{n=10};

    \addplot [
    color=red,
    solid,
    line width=1.5pt
    ]
    table{
    1.58873251442874 0.139270685555603
    1.75094674410523 0.187648610352166
    2.00056999517431 0.274533946251121
    2.27705604989095 0.374550491086161
    2.29596621491152 0.380969240114346
    2.41176717604408 0.417753266892818
    2.41614828744096 0.419044488634782
    2.45981848928711 0.431445200552556
    2.51461996380818 0.44569150663875
    2.63732800530387 0.471433199097579
    2.67540149916467 0.47748693150892
    2.72686458640717 0.484091305194831
    3.03140642922051 0.483465535521604
    3.07384878732821 0.477986374915163
    3.1895413994593 0.4569517259234
    3.25640618890004 0.441099033384993
    3.39991043346777 0.399612809225554
    3.7029148147097 0.292622994026572
    3.94491621538283 0.206344381423128
    4.36469219413918 0.0911022396340956
    4.68444030939428 0.0408178745872097
    };
    \addlegendentry{n=15};

    \addplot [
    color=mycolor1,
    solid,
    line width=1.5pt
    ]
    table{
    2.44469092422533 0.134047643683245
    2.99469910210473 0.332559909167666
    3.05373235366556 0.355888865309276
    3.31377765482139 0.447946402923478
    3.41066336498647 0.474278454032695
    3.73828133650772 0.512840458011838
    4.11146934521349 0.451592119764443
    4.12204843744243 0.448459021945289
    4.13062375797785 0.445874763291985
    4.34434429091486 0.371194128891267
    5.24212616032033 0.0753126892773911
    };
    \addlegendentry{n=20};

    \end{axis}
    \end{tikzpicture}%

    \end{document}

在此处输入图片描述

但:

如果我做

\documentclass{article}
\usepackage{tikz,pgfplots}
\usepackage{subfig}
\usepackage{tabularx}
\pgfplotsset{compat=newest}

\usepgfplotslibrary{external}
\tikzexternalize
\begin{document}


\begin{figure}[!ht]
  \centering\makebox[\textwidth]
  {\resizebox{1.2\textwidth}{!}{%
  \begin{tabularx}{1.2\textwidth}{*2X}
  \begin{center}
  \subfloat[Normal distribution at n=5,10,15,20]{\label{fig:normpdf_3d}\input{./Figures/chapter6/normpdf_3d.tex}}\\%width=6cm,height=7.59cm
  \subfloat[Probability frequency density plot at n=5,10,15,20]{\label{fig:normpdf_2d}\input{./Figures/chapter6/normpdf_2d.tex}}%width=7.38cm,height=4.97cm
   \end{center}
 \end{tabularx}}}
 \smallskip
  \caption{PDF and scatter plots}
  \label{fig:normpdf}
\end{figure}

\end{document}

我遇到了一些愚蠢的错误: 在此处输入图片描述

任何想法都会非常有帮助。

仅显示--shell-escape已打开。 在此处输入图片描述

重要提示:该文件编译正确地 没有 \usepgfplotslibrary{external} \tikzexternalize[原文如此]。我是否需要在调用的文件前言中添加一些内容?例如 \input{normpdf_3d.tex}?

答案1

  • 为了让您的生活更轻松,请mytikz为您的绘图创建一个包()。

    % mytikz.sty
    % ========================================================
    % Copyright (c) 2013 Your Name. All rights reservered.
    % LPPL LaTeX Public Project License
    % ========================================================
    \NeedsTeXFormat{LaTeX2e}[1994/06/01] 
    \ProvidesPackage{mytikz}[2013/03/28 v0.01 Package for my TikZ configuration]
    
    \RequirePackage{pgfplots}
    \definecolor{mycolor1}{rgb}{0,0.75,0.75}
    \pgfplotsset{compat=newest}
    \endinput
    
  • standalone在子包中使用文档类( myimage.tex)并加载mytikz包。

    % myimage.tex
    
    \documentclass[tikz,border=12pt]{standalone}
    \usepackage{mytikz}
    
    \begin{document}
    
    \begin{tikzpicture}[scale=0.75]
    
    \begin{axis}[%
    width=4.52083333333333in,
    height=3.565625in,
    scale only axis,
    xmin=0, xmax=6,
    xlabel={cfu (Y)},
    ymin=0, ymax=1,
    ylabel={Probability density},
    legend style={draw=black,fill=white,legend cell align=left}
    ]
    \addplot [
    color=blue,
    solid,
    line width=1.5pt
    ]
    table{
    0.164920894751145 0.145245471693298
    0.34898483521757 0.314105513485571
    0.400206868657802 0.375704063846473
    0.431202365194672 0.415565960090126
    0.468283168514601 0.465368046385591
    0.468819173063357 0.466102333522882
    0.541086963948335 0.567617590141928
    0.558035142464694 0.591815958738304
    0.558424656178589 0.592372256340699
    0.567763246195775 0.605704754250354
    0.588442297122976 0.635140815037755
    0.588968308388865 0.635887013738116
    0.589496900268574 0.636636709024852
    0.615575125108802 0.673361419528354
    0.656478830152384 0.729356371745445
    0.663734249176834 0.739001710618186
    0.681134429788414 0.761691427683771
    0.699206339854542 0.784509230520803
    0.703174714510729 0.789406773282464
    0.710293205919594 0.798082837552696
    0.738324318268112 0.830765143080553
    0.743729355370618 0.836772857231647
    0.7628624687978 0.857202030543563
    0.789650071275085 0.883431847212714
    0.848769035460717 0.93014828764704
    0.857054257851899 0.935346913150545
    0.867490832222506 0.941393771927579
    0.892221633180695 0.953428704494102
    0.910732799951076 0.96026629015755
    0.942439202692875 0.967534739622871
    0.964173551423469 0.969220797343017
    0.966490207360103 0.969241291138126
    0.972340307586979 0.969156381555276
    0.999815929196087 0.966143308438322
    1.01840762385923 0.96167588822653
    1.02422624912665 0.959879519150392
    1.04820841787849 0.950503937405069
    1.05040526585597 0.949488520832702
    1.05188064663965 0.948792015052305
    1.06421741151945 0.942513866116551
    1.06952744475091 0.939564533487357
    1.07377729763307 0.937098333090976
    1.10837980144772 0.913625640841771
    1.12655613304724 0.898983298828636
    1.13577066384758 0.89098627132941
    1.1580472276478 0.870142330490693
    1.15859589504013 0.869603007065865
    1.18051156035791 0.847100007296536
    1.21401730562959 0.809369022407148
    1.33027028384446 0.656408242977467
    1.33483660875034 0.649970375448687
    1.37629546726956 0.590995765951455
    1.39873034424263 0.558976024508442
    1.42011477374599 0.528607866819228
    1.49811243643743 0.421418636969251
    1.55782976646071 0.345795703108512
    1.60683136399288 0.289407680329164
    1.67872959183145 0.217237684951087
    1.70586524394617 0.193407783948843
    2.09859653835273 0.0221208957939459
    2.23050096301146 0.0087062390469457
    };
    \addlegendentry{n=5};
    
    \addplot [
    color=green!50!black,
    solid,
    line width=1.5pt
    ]
    table{
    1.11061716777245 0.25439255248339
    1.32152703852018 0.444545345197107
    1.33743602693433 0.460242989969573
    1.42547263125263 0.547313752669856
    1.50599624519356 0.623697850962722
    1.5261804928616 0.641779387498937
    1.573838875732 0.682042674974274
    1.57730219814673 0.684816482012706
    1.62699159305991 0.721956657982538
    1.63486458952984 0.727347067179288
    1.68630344515834 0.758807092685913
    1.71784982886711 0.774600265244482
    1.83289913575602 0.806644546215021
    1.86864417529802 0.807887335418516
    1.88963804306077 0.806645110709114
    1.90172633188334 0.805269969440086
    1.97361138903204 0.787332803216098
    2.16847522483986 0.665794197723055
    2.26107642525521 0.582137894844234
    2.44093902647085 0.40561167899897
    2.55728899719626 0.299162501222641
    2.72596462417501 0.174351769994033
    3.14460320890567 0.0275718440302502
    };
    \addlegendentry{n=10};
    
    \addplot [
    color=red,
    solid,
    line width=1.5pt
    ]
    table{
    1.58873251442874 0.139270685555603
    1.75094674410523 0.187648610352166
    2.00056999517431 0.274533946251121
    2.27705604989095 0.374550491086161
    2.29596621491152 0.380969240114346
    2.41176717604408 0.417753266892818
    2.41614828744096 0.419044488634782
    2.45981848928711 0.431445200552556
    2.51461996380818 0.44569150663875
    2.63732800530387 0.471433199097579
    2.67540149916467 0.47748693150892
    2.72686458640717 0.484091305194831
    3.03140642922051 0.483465535521604
    3.07384878732821 0.477986374915163
    3.1895413994593 0.4569517259234
    3.25640618890004 0.441099033384993
    3.39991043346777 0.399612809225554
    3.7029148147097 0.292622994026572
    3.94491621538283 0.206344381423128
    4.36469219413918 0.0911022396340956
    4.68444030939428 0.0408178745872097
    };
    \addlegendentry{n=15};
    
    \addplot [
    color=mycolor1,
    solid,
    line width=1.5pt
    ]
    table{
    2.44469092422533 0.134047643683245
    2.99469910210473 0.332559909167666
    3.05373235366556 0.355888865309276
    3.31377765482139 0.447946402923478
    3.41066336498647 0.474278454032695
    3.73828133650772 0.512840458011838
    4.11146934521349 0.451592119764443
    4.12204843744243 0.448459021945289
    4.13062375797785 0.445874763291985
    4.34434429091486 0.371194128891267
    5.24212616032033 0.0753126892773911
    };
    \addlegendentry{n=20};
    
    \end{axis}
    \end{tikzpicture}%
    
    \end{document} 
    
  • 在主文件( )中使用standalone(或)包并加载(无论将其放在加载(或)包之前还是之后,位置并不重要)。docmutemain.texmytikzstandalonedocmute

     % main.tex
    
     \documentclass{article}
    
     \usepackage{subfig}
     \usepackage{tabularx}
    
     \usepackage{standalone}% or \usepackage{docmute}
     \usepackage{mytikz}
    
     \begin{document}
    
    
      \begin{figure}[!ht]
      \centering\makebox[\textwidth]
      {\resizebox{1.2\textwidth}{!}{%
      \begin{tabularx}{1.2\textwidth}{*2X}
      \begin{center}
      \subfloat[Normal distribution at n=5,10,15,20]      {\label{fig:normpdf_3d}\input{myimage.tex}}\\%width=6cm,height=7.59cm
      \subfloat[Probability frequency density plot at n=5,10,15,20]{\label{fig:normpdf_2d}\input{myimage.tex}}%width=7.38cm,height=4.97cm
      \end{center}
      \end{tabularx}}}
      \smallskip
      \caption{PDF and scatter plots}
      \label{fig:normpdf}
      \end{figure}
    
      \end{document}
    

模拟

仅出于模拟目的,我将子myimage.tex内容、主main.tex内容和mytikz包内容放在一个输入文件中。子内容和包必须包含在filecontents*环境中!

\documentclass{article}

\usepackage{filecontents}

\begin{filecontents*}{mytikz.sty}
% mytikz.sty
% ========================================================
% Copyright (c) 2013 Your Name. All rights reservered.
% LPPL LaTeX Public Project License
% ========================================================
\NeedsTeXFormat{LaTeX2e}[1994/06/01] 
\ProvidesPackage{mytikz}[2013/03/28 v0.01 Package for my TikZ configuration]
\RequirePackage{tikz}
\RequirePackage{pgfplots}
\definecolor{mycolor1}{rgb}{0,0.75,0.75}

\endinput
\end{filecontents*}


\begin{filecontents*}{myimage.tex}
    \documentclass[tikz,border=12pt]{standalone}
        \usepackage{mytikz}
        \begin{document}
    \begin{tikzpicture}[scale=0.75]

    \begin{axis}[%
    width=4.52083333333333in,
    height=3.565625in,
    scale only axis,
    xmin=0, xmax=6,
    xlabel={cfu (Y)},
    ymin=0, ymax=1,
    ylabel={Probability density},
    legend style={draw=black,fill=white,legend cell align=left}
    ]
    \addplot [
    color=blue,
    solid,
    line width=1.5pt
    ]
    table{
    0.164920894751145 0.145245471693298
    0.34898483521757 0.314105513485571
    0.400206868657802 0.375704063846473
    0.431202365194672 0.415565960090126
    0.468283168514601 0.465368046385591
    0.468819173063357 0.466102333522882
    0.541086963948335 0.567617590141928
    0.558035142464694 0.591815958738304
    0.558424656178589 0.592372256340699
    0.567763246195775 0.605704754250354
    0.588442297122976 0.635140815037755
    0.588968308388865 0.635887013738116
    0.589496900268574 0.636636709024852
    0.615575125108802 0.673361419528354
    0.656478830152384 0.729356371745445
    0.663734249176834 0.739001710618186
    0.681134429788414 0.761691427683771
    0.699206339854542 0.784509230520803
    0.703174714510729 0.789406773282464
    0.710293205919594 0.798082837552696
    0.738324318268112 0.830765143080553
    0.743729355370618 0.836772857231647
    0.7628624687978 0.857202030543563
    0.789650071275085 0.883431847212714
    0.848769035460717 0.93014828764704
    0.857054257851899 0.935346913150545
    0.867490832222506 0.941393771927579
    0.892221633180695 0.953428704494102
    0.910732799951076 0.96026629015755
    0.942439202692875 0.967534739622871
    0.964173551423469 0.969220797343017
    0.966490207360103 0.969241291138126
    0.972340307586979 0.969156381555276
    0.999815929196087 0.966143308438322
    1.01840762385923 0.96167588822653
    1.02422624912665 0.959879519150392
    1.04820841787849 0.950503937405069
    1.05040526585597 0.949488520832702
    1.05188064663965 0.948792015052305
    1.06421741151945 0.942513866116551
    1.06952744475091 0.939564533487357
    1.07377729763307 0.937098333090976
    1.10837980144772 0.913625640841771
    1.12655613304724 0.898983298828636
    1.13577066384758 0.89098627132941
    1.1580472276478 0.870142330490693
    1.15859589504013 0.869603007065865
    1.18051156035791 0.847100007296536
    1.21401730562959 0.809369022407148
    1.33027028384446 0.656408242977467
    1.33483660875034 0.649970375448687
    1.37629546726956 0.590995765951455
    1.39873034424263 0.558976024508442
    1.42011477374599 0.528607866819228
    1.49811243643743 0.421418636969251
    1.55782976646071 0.345795703108512
    1.60683136399288 0.289407680329164
    1.67872959183145 0.217237684951087
    1.70586524394617 0.193407783948843
    2.09859653835273 0.0221208957939459
    2.23050096301146 0.0087062390469457
    };
    \addlegendentry{n=5};

    \addplot [
    color=green!50!black,
    solid,
    line width=1.5pt
    ]
    table{
    1.11061716777245 0.25439255248339
    1.32152703852018 0.444545345197107
    1.33743602693433 0.460242989969573
    1.42547263125263 0.547313752669856
    1.50599624519356 0.623697850962722
    1.5261804928616 0.641779387498937
    1.573838875732 0.682042674974274
    1.57730219814673 0.684816482012706
    1.62699159305991 0.721956657982538
    1.63486458952984 0.727347067179288
    1.68630344515834 0.758807092685913
    1.71784982886711 0.774600265244482
    1.83289913575602 0.806644546215021
    1.86864417529802 0.807887335418516
    1.88963804306077 0.806645110709114
    1.90172633188334 0.805269969440086
    1.97361138903204 0.787332803216098
    2.16847522483986 0.665794197723055
    2.26107642525521 0.582137894844234
    2.44093902647085 0.40561167899897
    2.55728899719626 0.299162501222641
    2.72596462417501 0.174351769994033
    3.14460320890567 0.0275718440302502
    };
    \addlegendentry{n=10};

    \addplot [
    color=red,
    solid,
    line width=1.5pt
    ]
    table{
    1.58873251442874 0.139270685555603
    1.75094674410523 0.187648610352166
    2.00056999517431 0.274533946251121
    2.27705604989095 0.374550491086161
    2.29596621491152 0.380969240114346
    2.41176717604408 0.417753266892818
    2.41614828744096 0.419044488634782
    2.45981848928711 0.431445200552556
    2.51461996380818 0.44569150663875
    2.63732800530387 0.471433199097579
    2.67540149916467 0.47748693150892
    2.72686458640717 0.484091305194831
    3.03140642922051 0.483465535521604
    3.07384878732821 0.477986374915163
    3.1895413994593 0.4569517259234
    3.25640618890004 0.441099033384993
    3.39991043346777 0.399612809225554
    3.7029148147097 0.292622994026572
    3.94491621538283 0.206344381423128
    4.36469219413918 0.0911022396340956
    4.68444030939428 0.0408178745872097
    };
    \addlegendentry{n=15};

    \addplot [
    color=mycolor1,
    solid,
    line width=1.5pt
    ]
    table{
    2.44469092422533 0.134047643683245
    2.99469910210473 0.332559909167666
    3.05373235366556 0.355888865309276
    3.31377765482139 0.447946402923478
    3.41066336498647 0.474278454032695
    3.73828133650772 0.512840458011838
    4.11146934521349 0.451592119764443
    4.12204843744243 0.448459021945289
    4.13062375797785 0.445874763291985
    4.34434429091486 0.371194128891267
    5.24212616032033 0.0753126892773911
    };
    \addlegendentry{n=20};

    \end{axis}
    \end{tikzpicture}

    \end{document}
\end{filecontents*}

\usepackage{subfig}
\usepackage{tabularx}

\usepackage{standalone}% or \usepackage{docmute}
\usepackage{mytikz}

\begin{document}

\begin{figure}[!ht]
  \centering\makebox[\textwidth]
  {\resizebox{1.2\textwidth}{!}{%
  \begin{tabularx}{1.2\textwidth}{*2X}
  \begin{center}
  \subfloat[Normal distribution at n=5,10,15,20]{\label{fig:normpdf_3d}\input{myimage.tex}}\\%width=6cm,height=7.59cm
  \subfloat[Probability frequency density plot at n=5,10,15,20]{\label{fig:normpdf_2d}\input{myimage.tex}}%width=7.38cm,height=4.97cm
   \end{center}
 \end{tabularx}}}
 \smallskip
  \caption{PDF and scatter plots}
  \label{fig:normpdf}
\end{figure}

\end{document}

相关内容