水平居中 TikZ 图片

水平居中 TikZ 图片

我有一些想要使用的数字,但由于第一个数字比第二个数字大,所以第二个数字会相对于第一个数字的下边缘放置。我的代码:

\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=0.9]
    \draw[->] (0, 0) -- (6, 0) node[right] {$z$};
    \draw[->] (0, 0) -- (0, 6) node[above] {$x$};
    \coordinate (a) at (5, 1);
    \node[fill=red, circle, scale=0.3, label=above:{$s$}] (s) at (5, 5) {};
    \node[fill=red, circle, scale=0.3, label=below:{$e$}] (e) at (1, 1) {};
    \draw (s) -- (e);
    \node[fill=red, circle, scale=0.3, label=above:{$m^*$}] (m*) at (2.293, 3.707) {};
    \node[fill=red, circle, scale=0.3, label=right:{$m$}] (m) at (3, 3) {};
    \draw (m) -- (m*);
    \draw[dashed] (s) -- (a) node[pos=0.5, label=right:{$\Delta x$}] -- (e) node[pos=0.5, label=below:{$\Delta z$}] {};
\end{tikzpicture}
\hspace{10mm}
\begin{tikzpicture}        
    \coordinate (ayy) at (4.293, 1);
    \coordinate (mstar) at (4.293, 3.707);
    \coordinate (emm) at (7, 1);
    \node[fill=red, circle, scale=0.3, label=above:{$m^*$}] (m*) at (mstar) {};
    \node[fill=red, circle, scale=0.3, label=right:{$m$}] (m) at (emm) {};
    \node[fill=none, inner sep=0, outer sep=0] (a) at (ayy) {};
    \draw (m) -- (m*) node[pos=0.6, label=right:{$d$}] {};
    \draw[dashed] (a) -- (m*) node[pos=0.5, label=left:{$d\cos\alpha$}] {};
    \draw[dashed] (a) -- (m) node[pos=0.5, label=below:{$d\sin\alpha$}] {};
    \pic[draw, "$\alpha$", angle eccentricity=1.2, angle radius=7mm] {angle=ayy--mstar--emm};
\end{tikzpicture}
\caption{Calculating $m^*$ (Case 1)}
\end{figure}

我目前拥有的: 我拥有的

我想要得到什么: 我想要得到什么

答案1

欢迎!有很多方法,我这里举两种例子:

  1. 使用\vcenter。也许最简单的使用方法就是将其置于数学模式。
  2. 仅使用一张图片并使用示波器。

在第二个示例中,我通过使用极坐标、使用解析器解析坐标、组合路径等稍微简化了代码。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{angles,quotes}
\begin{document}
\begin{figure}[h]
\centering
$\vcenter{\hbox{\begin{tikzpicture}[scale=0.9]
    \draw[->] (0, 0) -- (6, 0) node[right] {$z$};
    \draw[->] (0, 0) -- (0, 6) node[above] {$x$};
    \coordinate (a) at (5, 1);
    \node[fill=red, circle, scale=0.3, label=above:{$s$}] (s) at (5, 5) {};
    \node[fill=red, circle, scale=0.3, label=below:{$e$}] (e) at (1, 1) {};
    \draw (s) -- (e);
    \node[fill=red, circle, scale=0.3, label=above:{$m^*$}] (m*) at (2.293, 3.707) {};
    \node[fill=red, circle, scale=0.3, label=right:{$m$}] (m) at (3, 3) {};
    \draw (m) -- (m*);
    \draw[dashed] (s) -- (a) node[pos=0.5, label=right:{$\Delta x$}]{}
     -- (e) node[pos=0.5, label=below:{$\Delta z$}] {};
\end{tikzpicture}}}
\hspace{10mm}
\vcenter{\hbox{\begin{tikzpicture}
    \coordinate (ayy) at (4.293, 1);
    \coordinate (mstar) at (4.293, 3.707);
    \coordinate (emm) at (7, 1);
    \node[fill=red, circle, scale=0.3, label=above:{$m^*$}] (m*) at (mstar) {};
    \node[fill=red, circle, scale=0.3, label=right:{$m$}] (m) at (emm) {};
    \node[fill=none, inner sep=0, outer sep=0] (a) at (ayy) {};
    \draw (m) -- (m*) node[pos=0.6, label=right:{$d$}] {};
    \draw[dashed] (a) -- (m*) node[pos=0.5, label=left:{$d\cos\alpha$}] {};
    \draw[dashed] (a) -- (m) node[pos=0.5, label=below:{$d\sin\alpha$}] {};
    \pic[draw, "$\alpha$", angle eccentricity=1.2, angle radius=7mm] {angle=ayy--mstar--emm};
\end{tikzpicture}}}$
\end{figure}

\begin{figure}[h]
\centering
\begin{tikzpicture}[bullet/.style={fill=red, circle, scale=0.3, label=#1}]
  \begin{scope}[scale=0.9]
    \draw[<->] (0, 6) node[above] {$x$} |- (6, 0) node[right] {$z$};
    \coordinate (a) at (5, 1);
    \node[bullet=above:{$s$}] (s) at (5, 5) {};
    \node[bullet=below:{$e$}] (e) at (1, 1) {};
    \draw (s) -- node[fill=red, circle, scale=0.3, label=right:{$m$}] (m){}  (e)
        (m) ++ (135:1) 
        node[fill=red, circle, scale=0.3, label=above:{$m^*$}] (m*) {};
    \draw (m) -- (m*);
    \draw[dashed] (s) |- (e) node[pos=0.75,below] {$\Delta z$}
    node[pos=0.25,right](dx){$\Delta x$};
  \end{scope}
  \begin{scope}
    \path (dx)  ++ (3,{-1-1/sqrt(8)})  coordinate (a)
        ++ (0,{2+1/sqrt(2)}) 
        node[bullet=above:{$m^*$}] (m*) {}
        (a) ++ ({2+1/sqrt(2)},0)
        node[bullet=right:{$m$}] (m){};
    \draw (m) -- (m*);  
    \draw[dashed] (m*)  --  node[left] (dc){$d\cos\alpha$} (a)
        -- node[below]{$d\sin\alpha$} (m);
    \pic[draw, "$\alpha$", angle eccentricity=1.2, angle radius=7mm] 
    {angle=a--m*--m};
  \end{scope}
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

顺便说一句,本网站的通常格式是提供完整的最小工作示例 (MWE)。这些代码包含编译所需的所有内容,通常以 开头\documentclass并以 结尾\end{document}。就您而言,我不知道您使用的是哪个文档类,所以我不得不猜测,以及您正在加载哪些包(除了让您的代码片段运行所需的包之外)。您很可能已经加载了某些东西,其中包含以另一种方式解决您的问题的工具。

答案2

另一种可能性是将图像与选定的基线对齐。例如,对图像的代码进行稍微修改后,MWE 为:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{angles,
                calc, % new
                quotes}

\begin{document}
    \begin{figure}[h]
    \tikzset{dot/.style = {circle, fill=red, minimum size=3pt, inner sep=0pt}}% new
\begin{tikzpicture}[baseline=(X.center)] % <---
% caxes
    \draw[->] (0,0) -- (6, 0) node[right] {$z$};
    \draw[->] (0,0) -- (0, 6) node[above] {$x$};
% triangle
\node (s) [dot, label=$s$]          at (5,5) {};
\node (e) [dot, label=below:{$e$}]  at (1,1) {};
\draw (s) -- node (m) [dot, label=right:$m$] {} (e);
\node (m*) [dot, label=$m^*$] at ($(m)!10mm!90:(s)$) {};
    \node[fill=red, circle, scale=0.3, label=right:{$m$}] (m) at (3, 3) {};
\draw (m) -- (m*);
\draw[dashed] (e) -| (s) node[pos=0.25, label=below:$\Delta z$] {}
                         node (X) [pos=0.75, label=right:$\Delta x$] {}; % <---
\end{tikzpicture}
    \hfill
\begin{tikzpicture}[baseline=(X.center)] % <---
\node (m*) [dot, label=above:$m^*$] {};
\node (m)  [dot, label=right:$m$]   at (3,-3) {};
\draw (m*) -- node[label=right:$d$] {} (m);
\draw[dashed] (m*) |- (m)   node (X) [pos=0.25, label=left:$d\cos\alpha$] {} % <---
                            coordinate[pos=0.5] (a)  % <--- 
                            node[pos=0.75, label=below:$d\sin\alpha$] {};
\pic[draw, "$\alpha$", angle eccentricity=1.2, angle radius=7mm] {angle=a--m*--m};
\end{tikzpicture}
\end{figure}
\caption{Calculating $m^*$ (Case 1)}
\end{document}

在此处输入图片描述

相关内容