Tikz 图片未居中并向右移动,尽管位于中心

Tikz 图片未居中并向右移动,尽管位于中心

我面临如下图所示的问题

在此处输入图片描述

代码如下

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{center}
\begin{tikzpicture}

\path[draw, line width=1pt] (0,5) -- (4,5) -- (2,8.45) -- cycle;
\path[draw, line width=1pt] (7,5) -- (11,5) -- (9,8.45) -- cycle;
\path[draw, line width=1pt] (14,5) -- (18,5) -- (16,8.45) -- cycle;
\path[draw, line width=1pt] (0,0) -- (4,0) -- (2,3.45) -- cycle;
\path[draw, line width=1pt] (7,0) -- (11,0) -- (9,3.45) -- cycle;
\path[draw, line width=1pt] (14,0) -- (18,0) -- (16,3.45) -- cycle;

\end{tikzpicture}
\end{center}

\end{document}

我不知道为什么整个图片似乎都向右移动了,尽管放在\center环境中。似乎存在(显然)某种最大宽度。但我不知道如何\tikzpicture相应地调整环境。

两个问题:第一,为什么 tikz-picture 会在左边距之后开始?第二,为什么 tikz-picture 会延伸到右边框?

提前致谢!

附加问题:如果有人能解释如何使三角形真正等边,那就太好了!

答案1

您的图片太宽(超过 的值\textline)。您应该收到类似以下警告Overfull \hbox (168.54938pt too wide) in paragraph at lines 30--31

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother

\begin{document}

\begin{center}
Your textline is \convertto{cm}{\the\textwidth} cm wide.

\begin{tikzpicture}
\path[draw, line width=1pt] (0,5) -- (4,5) -- (2,8.45) -- cycle;
\path[draw, line width=1pt] (7,5) -- (11,5) -- (9,8.45) -- cycle;
\path[draw, line width=1pt] (14,5) -- (18,5) -- (16,8.45) -- cycle;
\path[draw, line width=1pt] (0,0) -- (4,0) -- (2,3.45) -- cycle;
\path[draw, line width=1pt] (7,0) -- (11,0) -- (9,3.45) -- cycle;
\path[draw, line width=1pt] (14,0) -- (18,0) -- (16,3.45) -- cycle;
\draw (current bounding box.south east) -- (current bounding box.north east)
  -- (current bounding box.north west) -- (current bounding box.south west)
  -- cycle;
\path let \p1 = (current bounding box.south east) in
  let \p2 = (current bounding box.south west) in
  let \n1 = {\x1 - \x2} in
  \pgfextra{\pgfmathsetmacro{\temp}{\n1 / 1cm}}
  (current bounding box.south) node[below] {Your picture is \temp cm wide};

\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

使用比例选项使图片适合线条宽度。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\def\convertto#1#2{\strip@pt\dimexpr #2*65536/\number\dimexpr 1#1}
\makeatother

\begin{document}

\begin{center}
Your textline is \convertto{cm}{\the\textwidth} cm wide.

\begin{tikzpicture}
\begin{scope}[scale=0.6]
  \path[draw, line width=1pt] (0,5) -- (4,5) -- (2,8.45) -- cycle;
  \path[draw, line width=1pt] (7,5) -- (11,5) -- (9,8.45) -- cycle;
  \path[draw, line width=1pt] (14,5) -- (18,5) -- (16,8.45) -- cycle;
  \path[draw, line width=1pt] (0,0) -- (4,0) -- (2,3.45) -- cycle;
  \path[draw, line width=1pt] (7,0) -- (11,0) -- (9,3.45) -- cycle;
  \path[draw, line width=1pt] (14,0) -- (18,0) -- (16,3.45) -- cycle;
\end{scope}
\draw (current bounding box.south east) -- (current bounding box.north east)
  -- (current bounding box.north west) -- (current bounding box.south west)
  -- cycle;
\path let \p1 = (current bounding box.south east) in
  let \p2 = (current bounding box.south west) in
  let \n1 = {\x1 - \x2} in
  \pgfextra{\pgfmathsetmacro{\temp}{\n1 / 1cm}}
  (current bounding box.south) node[below] {Your picture is \temp cm wide};

\end{tikzpicture}
\end{center}

\end{document}

在此处输入图片描述

相关内容