2

How can I vertically align the axes of two graphs drawn in tikz side by side? Here is my code:

\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}

As you can see in the picture below, the axes are slightly misaligned: enter image description here The problem seems to be coming from the fact that the g in 'Long' makes the left title taller. I have tried defining the same resize dimensions for both pictures and valign = t but both of these did not work.

1
  • 1
    add the baseline \begin{tikzpicture}[baseline={(0,0)}]
    – AndréC
    Sep 13, 2020 at 6:56

3 Answers 3

3

Simply position the baseline on the same coordinate, here (0,0):

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

screenshot

\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}
3
  • Small vertical miss align of images is caused by different vertical size of title nodes: the first contain letter g with descender, the second hasn't such letter. If you prescribe text depth, for example with text depth=0.5ex, both title nodes will have the same height, consequently both images become vertical aligned.
  • Off-topic: use of \resizebox is not good idea. It changes fonts size too (so you lost control over them).
\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}

enter image description here

(red lines shows text borders)

0

One slightly more generic workaround for when different text heights in headings or captions ruin vertical alignment is to make Latex pretend there is also a higher (lower) letter in the other text.

If your two texts are Long Call and Short Call, just change the latter to Short Call\vphantom{g}. This way Latex will treat it as also taking up this vertical space (but no additional horizontal space).

Note that this is a bit of a hack and there is almost always a better solution, such as changing the origin in this case, but it is a useful trick if you want to quickly fix the alignment. You just need to know/guess which letters are causing it, or use the slightly overkill Short Call\vphantom{Long Call}, just to be sure.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .