为什么无法为 \begin{tikzpicture}[>=stealth] 添加图形标题

为什么无法为 \begin{tikzpicture}[>=stealth] 添加图形标题

这是来自如何绘制折线图

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}    
\tikzset{%
pics/lines/.style args={#1-#2-#3}{
    code={
   \ifnum #3>1000 
   \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{max}$};
   \else 
   \ifnum #3=0
    \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{min}$};
   \else 
       \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{#1}$};
       \fi
       \fi
    }
  }
}

\begin{document}

\begin{tikzpicture}[>=latex]
\draw [<->] (0,4)node[right]{$y$}|-(8,0)node[right]{$x$};
\pic {lines={1-1-1}};
\pic {lines={2-2-1}};
\pic {lines={3-1-1}};
\pic {lines={4-3-1001}}; % <-- Max Value > 1000
\pic {lines={5-1-1}};
\pic {lines={6-0.5-0}};% <-- Min Value = 0
\pic {lines={7-1.5-1}};
\end{tikzpicture}
\end{document}

当我添加标题时,它不起作用并出现错误。

\begin{figure}[h!]
\centering
...
\caption{Scdsadasdsities}
\label{fig:Scdsadasdsities}
\end{figure}

在此处输入图片描述

\documentclass[margin=3mm]{standalone}

\usepackage{tikz}

\tikzset{%
pics/lines/.style args={#1-#2-#3}{
    code={
   \ifnum #3>1000 
   \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{max}$};
   \else 
   \ifnum #3=0
    \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{min}$};
   \else 
       \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{#1}$};
       \fi
       \fi
    }
  }
}

\begin{document}

\begin{figure}[h!]
\centering

\begin{tikzpicture}[>=stealth]
\draw [<->] (0,4)node[right]{$y$}|-(8,0)node[right]{$x$};
\pic {lines={1-1-1}};
\pic {lines={2-2-1}};
\pic {lines={3-1-1}};
\pic {lines={4-3-1001}}; % <-- Max Value > 1000
\pic {lines={5-1-1}};
\pic {lines={6-0.5-0}};% <-- Min Value = 0
\pic {lines={7-1.5-1}};
\end{tikzpicture}

\caption{Scdsadasdsities}
\label{fig:Scdsadasdsities}

\end{figure}



\end{document}

答案1

您可以使用独立的类选项预览。

平均能量损失

\documentclass[preview, margin=3mm]{standalone}

\usepackage{tikz}

\tikzset{%
pics/lines/.style args={#1-#2-#3}{
    code={
   \ifnum #3>1000 
   \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{max}$};
   \else 
   \ifnum #3=0
    \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{min}$};
   \else 
       \draw [->,densely dashed](#1,0)--++(90:#2)node[above]{$x_{#1}$};
       \fi
       \fi
    }
  }
}

\begin{document}

\begin{figure}[h!]
\centering

\begin{tikzpicture}[>=stealth]
\draw [<->] (0,4)node[right]{$y$}|-(8,0)node[right]{$x$};
\pic {lines={1-1-1}};
\pic {lines={2-2-1}};
\pic {lines={3-1-1}};
\pic {lines={4-3-1001}}; % <-- Max Value > 1000
\pic {lines={5-1-1}};
\pic {lines={6-0.5-0}};% <-- Min Value = 0
\pic {lines={7-1.5-1}};
\end{tikzpicture}

\caption{Scdsadasdsities}
\label{fig:Scdsadasdsities}

\end{figure}



\end{document}

结果

在此处输入图片描述

相关内容