5

I have a standalone document that contains two plots. See MWE:

\documentclass[tikz]{standalone}

% PACKAGES LOADING

\usepackage{pgfplots} % To draw plots.

% TIKZ & PGFPLOTS LIBRARIES & SETTINGS

\pgfplotsset{compat=1.6} \usepgfplotslibrary{fillbetween} \usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}

\pgfplotsset{compat=1.17} \usetikzlibrary{arrows.meta, intersections, positioning} \pgfplotsset{width=6cm, height=6cm, % <--- axis lines=middle, xlabel={$\cdot$}, xlabel style={anchor=west}, ylabel={$u(\cdot)$}, ylabel style={anchor=south}, xtick=\empty, ytick=\empty, clip=false, ylabel style={overlay}, yticklabel style={overlay}, } \tikzset{ ddline/.append style = {draw=gray, densely dotted}, tick/.style = {inner sep=2pt, font=\footnotesize, align=right} }

% FIGURES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

% Figure 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tikzpicture}[baseline] \begin{axis}[ axis x line shift={-ln(0.1)}, % <--- xmin = 0, domain = 0.1:5, samples = 100, scale = 1.5 ] \addplot [thick] {ln(\x)}; % \draw[ddline]
(0.5,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x$} |- coordinate[pos=0.5] (ux) (0,{ln(0.5)}) node[tick,left] {$u(x)$}; \draw[ddline, name path=B] (2,{ln(0.1)}) node[tick,below,xshift=20pt] {$px+(1-p)y$} |- (0,{ln(2)}) node[tick,left] {$u(px+(1-p)y)$}; \draw[ddline]
(4,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$y$} |- coordinate[pos=0.5] (uy) (0,{ln(4)}) node[tick,left] {$u(y)$}; \draw[name path=A, semitransparent] (ux) -- (uy); \draw[name intersections={of=A and B, by=s}, ddline] (s) -- (s -| 1.2,1.2) node[tick,left] {}; \draw[ddline] (1.2,{ln(0.1)}) node[tick,below] {$c(F,u)$} |- (0,{ln(1.22)}) node[tick,left] {$pu(x)+(1-p)u(y)$}; \end{axis} \end{tikzpicture}%

% Figure 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tikzpicture}[baseline] \begin{axis}[ axis x line shift={-ln(0.1)}, % <--- xmin = 0, domain = 0.1:5, samples = 100, scale = 1.5, ] \addplot [thick] {ln(\x)}; % \draw[ddline]
(0.5,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x-\varepsilon$} |- coordinate[pos=0.5] (ux) (0,{ln(0.5)}) node[tick,left] {$u(x-\varepsilon)$}; \draw[ddline, name path=B] (2,{ln(0.1)}) node[tick,below] {$(b)$} |- (0,{ln(1.22)}) node[tick,left] {$(a)=u(x)$}; \draw[ddline]
(4,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x+\varepsilon$} |- coordinate[pos=0.5] (uy) (0,{ln(4)}) node[tick,left] {$u(x+\varepsilon)$}; \draw[name path=A, semitransparent] (ux) -- (uy); \draw[ddline] (1.2,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x$} |- (1.2,{ln(1.22)}) node[tick,left] {}; \end{axis} \end{tikzpicture}%

\end{document}

I then include these plots in my main .tex file via \includepraphics[]{} as follows:

\documentclass{article}

% PACKAGES LOADING

\usepackage{graphicx}

% DOCUMENT ITSELF %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\begin{figure} \centering \includegraphics[page=1]{MWE 1.pdf} \caption{Caption} \end{figure}

\begin{figure} \centering \includegraphics[page=2]{MWE 1.pdf} \caption{Caption} \end{figure}

\end{document}

As you can see in the picture, the plots are not aligned by the y-axis, as I desire. I've seen questions that are similar to mine, but none of them solves my issue. Some answers use the trim axis option, but that does not work well in standalone document class because it erases whatever is on left of the y-axis. I have also tried to play with baseline, overlay and use as bounding box options to no avail. How can I align these plots by their y-axis?

enter image description here

EoDmnFOr3q
  • 2,299
  • 2
    While LaTeX keeps track of the baseline using separate height and depth, no such facility exists horizontally. What you CAN do is force every pgfplot to have the same margins relative to the origin (lower left corner). See https://tex.stackexchange.com/questions/558495/pgfplots-trim-axis-in-tikzpicture-messes-up-legend-position and https://tex.stackexchange.com/questions/321470/subfloat-and-pgfplot-with-trim/321550?r=SearchResults&s=2|41.6007#321550 – John Kormylo Feb 26 '21 at 13:40
  • 3
    A workaround is to add a phantom node in the second tikzpicture: \draw (0,0) node[tick,left] {\phantom{$pu(x)+(1-p)u(y)$}}; – Ivan Feb 26 '21 at 13:56
  • Although Kormylo's solution seems more 'Latexy', I have adopted Ivan's suggestion because of its simplicity (despite not being as elegant). if you post it as an answer, I will accept it. – EoDmnFOr3q Feb 26 '21 at 20:31
  • 1
    @Ivan, do you want to write an answer or shall we close the question as answered in the comments? – Stefan Pinnow Mar 09 '21 at 12:19
  • 2
    You can use \pgfplotsset{yticklabel style={text width=<width>,align=right}} to specify the amount of space the y labels are to take. See Aligning subplots in a pgfplots figure. – Peter Grill Mar 09 '21 at 18:36
  • @PeterGrill I think you should post this comment as answer for other who may come across this question in the future. – EoDmnFOr3q Mar 09 '21 at 19:41
  • 1
    @Hector: I would but for some reason the given code does not produce the results shown for me. Also, have noticed that the y-axis labels are manually placed as tikz nodes, not as axis labels. But placing just one label should resolve the issue. – Peter Grill Mar 10 '21 at 02:23

1 Answers1

1

A workaround is to add a phantom node in the second tikzpicture:

\draw (0,0)   node[tick,left] {\phantom{$pu(x)+(1-p)u(y)$}};

MWE

\documentclass[tikz]{standalone}

% PACKAGES LOADING

\usepackage{pgfplots} % To draw plots.

% TIKZ & PGFPLOTS LIBRARIES & SETTINGS

\pgfplotsset{compat=1.6} \usepgfplotslibrary{fillbetween} \usetikzlibrary{fit, calc, matrix, positioning, arrows.meta, intersections, through, backgrounds, patterns}

\pgfplotsset{compat=1.17} \usetikzlibrary{arrows.meta, intersections, positioning} \pgfplotsset{width=6cm, height=6cm, % <--- axis lines=middle, xlabel={$\cdot$}, xlabel style={anchor=west}, ylabel={$u(\cdot)$}, ylabel style={anchor=south}, xtick=\empty, ytick=\empty, clip=false, ylabel style={overlay}, yticklabel style={overlay}, } \tikzset{ ddline/.append style = {draw=gray, densely dotted}, tick/.style = {inner sep=2pt, font=\footnotesize, align=right} }

% FIGURES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

% Figure 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tikzpicture}[baseline] \begin{axis}[ axis x line shift={-ln(0.1)}, % <--- xmin = 0, domain = 0.1:5, samples = 100, scale = 1.5 ] \addplot [thick] {ln(\x)}; % \draw[ddline]
(0.5,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x$} |- coordinate[pos=0.5] (ux) (0,{ln(0.5)}) node[tick,left] {$u(x)$}; \draw[ddline, name path=B] (2,{ln(0.1)}) node[tick,below,xshift=20pt] {$px+(1-p)y$} |- (0,{ln(2)}) node[tick,left] {$u(px+(1-p)y)$}; \draw[ddline]
(4,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$y$} |- coordinate[pos=0.5] (uy) (0,{ln(4)}) node[tick,left] {$u(y)$}; \draw[name path=A, semitransparent] (ux) -- (uy); \draw[name intersections={of=A and B, by=s}, ddline] (s) -- (s -| 1.2,1.2) node[tick,left] {}; \draw[ddline] (1.2,{ln(0.1)}) node[tick,below] {$c(F,u)$} |- (0,{ln(1.22)}) node[tick,left] {$pu(x)+(1-p)u(y)$}; \end{axis} \end{tikzpicture}%

% Figure 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tikzpicture}[baseline] \begin{axis}[ axis x line shift={-ln(0.1)}, % <--- xmin = 0, domain = 0.1:5, samples = 100, scale = 1.5, ] \addplot [thick] {ln(\x)}; % \draw (0,0) node[tick,left] {\phantom{$pu(x)+(1-p)u(y)$}}; \draw[ddline]
(0.5,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x-\varepsilon$} |- coordinate[pos=0.5] (ux) (0,{ln(0.5)}) node[tick,left] {$u(x-\varepsilon)$}; \draw[ddline, name path=B] (2,{ln(0.1)}) node[tick,below] {$(b)$} |- (0,{ln(1.22)}) node[tick,left] {$(a)=u(x)$}; \draw[ddline]
(4,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x+\varepsilon$} |- coordinate[pos=0.5] (uy) (0,{ln(4)}) node[tick,left] {$u(x+\varepsilon)$}; \draw[name path=A, semitransparent] (ux) -- (uy); \draw[ddline] (1.2,{ln(0.1)}) node[tick,below,yshift=-1.97pt] {$x$} |- (1.2,{ln(1.22)}) node[tick,left] {}; \end{axis} \end{tikzpicture}%

\end{document}

enter image description here

Ivan
  • 4,368