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}

在此輸入影像描述

相關內容