Tikz 圖片不垂直對齊

Tikz 圖片不垂直對齊

如何垂直對齊在 tikz 中並排繪製的兩個圖表的軸?這是我的程式碼:

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{center}
        \resizebox{0.45\linewidth}{8cm}{
            \begin{tikzpicture}
            \tikzstyle{every node}=[font=\tiny];
            
            %Axes
            \draw[-latex] (0, 0) -- (3, 0) ;
            \draw[-latex] (0, -1.5) -- (0, 2);
            \node[above] at (0, 2) {\( \$ \)};
            \node[right] at (3, 0) {\( S_T \)}; 

            %Title
            \node[above, font = \small\bfseries] at (current bounding box.north) {Long Call};
            \end{tikzpicture}
        }
        \quad
        \resizebox{0.45\linewidth}{8cm}{
            \begin{tikzpicture}
            \tikzstyle{every node}=[font=\tiny];
            
            %Axes
            \draw[-latex] (0, 0) -- (3, 0) ;
            \draw[-latex] (0, -1.5) -- (0, 2);
            \node[above] at (0, 2) {\( \$ \)};
            \node[right] at (3, 0) {\( S_T \)}; 

            %Title
            \node[above, font = \small\bfseries] at (current bounding box.north) {Short Call};
        \end{tikzpicture}
        }
    \end{center}
\end{document}

如下圖所示,軸心稍微未對齊: 在此輸入影像描述 問題似乎出在“Long”中的 g 使左側標題更高的事實。我嘗試為圖片和 valign = t 定義相同的調整大小尺寸,但這兩者都不起作用。

答案1

只需將其放置baseline在同一座標上即可(0,0)

\begin{tikzpicture}[baseline={(0,0)}]

螢幕截圖

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{center}
        \resizebox{0.45\linewidth}{8cm}{
            \begin{tikzpicture}[baseline={(0,0)}]
            \tikzstyle{every node}=[font=\tiny];
            
            %Axes
            \draw[-latex] (0, 0) -- (3, 0) ;
            \draw[-latex] (0, -1.5) -- (0, 2);
            \node[above] at (0, 2) {\( \$ \)};
            \node[right] at (3, 0) {\( S_T \)}; 

            %Title
            \node[above, font = \small\bfseries] at (current bounding box.north) {Long Call};
            \end{tikzpicture}
        }
        \quad
        \resizebox{0.45\linewidth}{8cm}{
            \begin{tikzpicture}[baseline={(0,0)}]
            \tikzstyle{every node}=[font=\tiny];
            
            %Axes
            \draw[-latex] (0, 0) -- (3, 0) ;
            \draw[-latex] (0, -1.5) -- (0, 2);
            \node[above] at (0, 2) {\( \$ \)};
            \node[right] at (3, 0) {\( S_T \)}; 

            %Title
            \node[above, font = \small\bfseries] at (current bounding box.north) {Short Call};
        
        \end{tikzpicture}
        }
    \end{center}
\end{document}

答案2

  • 影像的小垂直未對齊是由標題節點的垂直尺寸不同引起的:第一個包含字母 g 帶下行字母,第二個沒有這樣的字母。如果您指定文字深度,例如使用text depth=0.5ex,則兩個標題節點將具有相同的高度,因此兩個圖像都會垂直對齊。
  • 題外話:使用\resizebox不是一個好主意。它也會改變字體大小(所以你失去了對它們的控制)。
\documentclass{article}
\usepackage{tikz}
%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{center}
\tikzset{
every node/.style = {text depth=0.5ex, font=\small} % <---
        }
    \begin{tikzpicture}
%Axes
\draw[-latex] (0, 0-0) -- (0.4\linewidth, 0) node[right] {\( S_T \)}; 
\draw[-latex] (0,-1.5) -- (0, 3) node[above] {\( \$ \)};
%Title
\node[above, font = \bfseries] at (current bounding box.north) {Long Call};
    \end{tikzpicture}
\hfill
    \begin{tikzpicture}
%Axes
\draw[-latex] (0, 0-0) -- (0.4\linewidth, 0) node[right] {\( S_T \)};
\draw[-latex] (0,-1.5) -- (0, 3) node[above] {\( \$ \)};
%Title
\node[above, font = \bfseries] at (current bounding box.north) {Short Call};
\end{tikzpicture}
    \end{center}
\end{document}

在此輸入影像描述

(紅線顯示文字邊框)

答案3

當標題或說明中的不同文字高度破壞垂直對齊時,一種稍微更通用的解決方法是讓 Latex 假裝其他文字中也有一個更高(更低)的字母。

如果你的兩個文本是Long CallShort Call,只需將後者更改為Short Call\vphantom{g}。這樣,Latex 就會將其視為也佔據了這個垂直空間(但沒有額外的水平空間)。

請注意,這有點像駭客,幾乎總是有更好的解決方案,例如在這種情況下更改原點,但如果您想快速修復對齊,這是一個有用的技巧。您只需要知道/猜測哪些字母導致了它,或者使用稍微矯枉過正的方法Short Call\vphantom{Long Call},只是為了確定。

相關內容