
tikz で描画された 2 つのグラフの軸を垂直に並べて配置するにはどうすればよいですか? これが私のコードです:
\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 が含まれていますが、2 番目のノードにはそのような文字がありません。たとえば、 でテキストの深さを指定すると
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 に他のテキストにも高い (低い) 文字があると思わせることです。
2 つのテキストが と の場合はLong Call
、Short Call
後者を に変更するだけですShort Call\vphantom{g}
。この方法では、Latex はそれをこの垂直方向のスペースも占有するものとみなします (ただし、水平方向のスペースは追加されません)。
これはちょっとしたハックであり、この場合は原点を変更するなど、ほとんどの場合、より良い解決策がありますが、配置をすばやく修正したい場合には便利なトリックです。どの文字が原因であるかを知る/推測するか、Short Call\vphantom{Long Call}
念のため少しやり過ぎな方法を使用するだけです。