如何在 TikZ 中对齐事物

如何在 TikZ 中对齐事物

今天我正在做一个项目,在 TikZ 中很难对齐一些东西。我对 LaTeX 和 TikZ 还很陌生,所以如果有人能看看并提供帮助那就太好了。

\begin{center}
\begin{tikzpicture}
\vspace{5cm}
  \shade[ball color = gray!40, opacity = 0.4] (0,0) circle (1cm);
  \draw (0,0) circle (1cm);
  \draw (-1,0) arc (180:360:1 and 0.3);
  \draw[dashed] (1,0) arc (0:180:1 and 0.3);
  \fill[fill=black] (0,0) circle (1pt);
  \draw[dashed] (0,0 ) -- node[above]{$1$} (1,0);
  \hspace*{3\linewidth}
\end{tikzpicture}
\begin{tikzpicture}
  \shade[ball color = gray!40, opacity = 0.4] (0,0) circle (2cm);
  \draw (0,0) circle (2cm);
  \draw (-2,0) arc (180:360:2 and 0.6);
  \draw[dashed] (2,0) arc (0:180:2 and 0.6);
  \fill[fill=black] (0,0) circle (1pt);
  \draw[dashed] (0,0 ) -- node[above]{$2$} (2,0);
\end{tikzpicture}
\end{center}

我不知道如何让球体垂直对齐并在它们之间留出水平距离。如果可以的话请帮忙!

答案1

欢迎来到 TeX.SE!!!

一种可能性是在同一环境中绘制两个球体tikzpicture。然后您可以创建一个scope并移动其中一个球体的坐标原点。

像这样:

\documentclass[tikz,border=2mm]{standalone}

\begin{document}
\begin{tikzpicture}
  % left sphere
  \shade[ball color = gray!40, opacity = 0.4] (0,0) circle (1cm);
  \draw (0,0) circle (1cm);
  \draw (-1,0) arc (180:360:1 and 0.3);
  \draw[dashed] (1,0) arc (0:180:1 and 0.3);
  \fill[fill=black] (0,0) circle (1pt);
  \draw[dashed] (0,0 ) -- node[above]{$1$} (1,0);
  % right sphere, shifting the origin to the point (4,0)
  \begin{scope}[shift={(4,0)}]
    \shade[ball color = gray!40, opacity = 0.4] (0,0) circle (2cm);
    \draw (0,0) circle (2cm);
    \draw (-2,0) arc (180:360:2 and 0.6);
    \draw[dashed] (2,0) arc (0:180:2 and 0.6);
    \fill[fill=black] (0,0) circle (1pt);
    \draw[dashed] (0,0 ) -- node[above]{$2$} (2,0);
  \end{scope}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

将选项添加baseline={(0,0)}到您的tikzpictures。花括号是必需的,因为(0,0)包含逗号,否则会被误认为是选项之间的分隔符。该baseline选项指定用于水平对齐的点。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{center}
\begin{tikzpicture}[baseline={(0,0)}]
  \shade[ball color = gray!40, opacity = 0.4] (0,0) circle (1cm);
  \draw (0,0) circle (1cm);
  \draw (-1,0) arc (180:360:1 and 0.3);
  \draw[dashed] (1,0) arc (0:180:1 and 0.3);
  \fill[fill=black] (0,0) circle (1pt);
  \draw[dashed] (0,0 ) -- node[above]{$1$} (1,0);
  \hspace*{3\linewidth}
\end{tikzpicture}
\begin{tikzpicture}[baseline={(0,0)}]
  \shade[ball color = gray!40, opacity = 0.4] (0,0) circle (2cm);
  \draw (0,0) circle (2cm);
  \draw (-2,0) arc (180:360:2 and 0.6);
  \draw[dashed] (2,0) arc (0:180:2 and 0.6);
  \fill[fill=black] (0,0) circle (1pt);
  \draw[dashed] (0,0 ) -- node[above]{$2$} (2,0);
\end{tikzpicture}
\end{center}
\end{document}

相关内容