无法将 tikz 图片居中

无法将 tikz 图片居中

对于下面提到的 tikz 图片,我无法将其放在中心。请帮忙。

\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=black!10]
\tikzstyle{arrow} = [thick,->,>=stealth]

\begin{tikzpicture}[node distance=2cm, auto]

\node (start) [startstop] {Preparation of Tensile Specimen};
\node (in1) [startstop] [below of=start, xshift=0cm] {Tensile Testing};
\node (in2) [startstop] [below of=in1, xshift=0cm] {Analysis of Tensile Test Data for Sy};
\node (in3) [startstop] [below of=in2, xshift=0cm] {Preparation of High Cycle Fatigue Specimen};
\node (in4) [startstop] [below of=in3, xshift=0cm] {High Cycle Fatigue Testing};
\node (stop) [startstop] [below of=in4, xshift=0cm] {Analysis of Test Data};
\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (in2);
\draw [arrow] (in2) -- (in3);
\draw [arrow] (in3) -- (in4);
\draw [arrow] (in4) -- (stop);

\end{tikzpicture}

答案1

如果您的图片位于浮动环境之外(例如,图形),请确保添加\noindent后将\makebox[\textwidth]{ .. }图像置于中央,而不管边距有多宽。

\documentclass{article}
\usepackage{tikz}
\usepackage{showframe}
\begin{document}

\begin{center}
center
\end{center}

\tikzstyle{startstop} = [rectangle, rounded corners, minimum width=3cm, minimum height=1cm,text centered, draw=black, fill=black!10]
\tikzstyle{arrow} = [thick,->,>=stealth]

\noindent\makebox[\textwidth]{%
\begin{tikzpicture}[node distance=2cm, auto]
\node (start) [startstop] {Preparation of Tensile Specimen};
\node (in1) [startstop] [below of=start, xshift=0cm] {Tensile Testing};
\node (in2) [startstop] [below of=in1, xshift=0cm] {Analysis of Tensile Test Data for Sy};
\node (in3) [startstop] [below of=in2, xshift=0cm] {Preparation of High Cycle Fatigue Specimen};
\node (in4) [startstop] [below of=in3, xshift=0cm] {High Cycle Fatigue Testing};
\node (stop) [startstop] [below of=in4, xshift=0cm] {Analysis of Test Data};
\draw [arrow] (start) -- (in1);
\draw [arrow] (in1) -- (in2);
\draw [arrow] (in2) -- (in3);
\draw [arrow] (in3) -- (in4);
\draw [arrow] (in4) -- (stop);
\end{tikzpicture}
}

\end{document}

在此处输入图片描述

答案2

由于某种原因,您的图片是如何包含在文档中的,未知,所以无法建议您做什么。无论如何,看看以下对您的图片及其包含在文档中的重新设计是否可以帮助您:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, chains, positioning}  
\usepackage[skip=1ex]{caption}

\begin{document}
   \begin{figure}[htb]
   \centering
\begin{tikzpicture}[
node distance = 5mm,
  start chain = going below,
   box/.style = {rectangle, rounded corners, draw=black, fill=black!10,
                text width=3cm, minimum height=1cm, align=center,
                on chain, join=by arr},
   arr/.style = {thick, -Stealth}
                    ]
    \begin{scope}[every node/.style = {box}]
\node (start)   {Preparation of Tensile Specimen};
\node (in1)     {Tensile Testing};
\node (in2)     {Analysis of Tensile Test Data for Sy};
\node (in3)     {Preparation of High Cycle Fatigue Specimen};
\node (in4)     {High Cycle Fatigue Testing};
\node (stop)    {Analysis of Test Data};
    \end{scope}
\end{tikzpicture}
    \caption{Flowchart \dots}
    \label{fig:flowchart}
    \centering
    \end{figure}
\end{document}

在此处输入图片描述

(红线显示页面布局)

  • 如果您希望节点中的文本只占一行,则将其替换text width=3cmminimum width=3cm
  • 在上面的 mwe 中我使用了库arrows. metachains以及它的宏join=by ...和定位。使用它们可以使代码变得更短、更简洁。
  • 如果该图像是完整的(没有其他节点,也没有它们之间的连接),那么您可以省略所有节点名称,从而使其代码更短。

编辑: 考虑到上述注释,向图中添加了注释以及新的代码示例:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, chains, positioning}
\usepackage[skip=1ex]{caption}

\begin{document}
    \begin{figure}[htb]
    \centering
 \begin{tikzpicture}[
node distance = 5mm,
  start chain = going below,
   box/.style = {rectangle, rounded corners, draw=black, fill=black!10,
                 minimum width=3cm, minimum height=1cm, align=center,
                on chain, join=by arr},
   arr/.style = {thick, -Stealth}
                    ]
    \begin{scope}[every node/.style = {box}]
\node   {Preparation of Tensile Specimen};
\node   {Tensile Testing};
\node   {Analysis of Tensile Test Data for Sy};
\node   {Preparation of High Cycle Fatigue Specimen};
\node   {High Cycle Fatigue Testing};
\node   {Analysis of Test Data};
    \end{scope}
\end{tikzpicture}
    \caption{Flowchart \dots}
    \label{fig:flowchart}
    \end{figure}
\end{document}

相关内容