1

I am using the following code to get two figures side by side. The code (MWE) is:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{adjustbox}
\usepackage{float}  
\begin{document}
\begin{figure}
\begin{minipage}[b]{0.49\textwidth}
    \begin{figure}[H]
    \begin{adjustbox}{width=1\textwidth}{
    \begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0.1:5}, my grid/.style={densely dotted,opacity=0.5, every node/.style={black,opacity=1}}, my axis/.style={latex-latex}]
    \draw[my plot, color=black] (0,0) plot (\x,{ln(\x)});
    \coordinate (start plot) at (0.1,{ln(0.1)});
    \coordinate (end plot) at (5,{ln(5)});
    \draw[my axis] ([shift={(-0.5cm,0.5cm)}]start plot |- end plot) node[above] {$u(\cdot)$} |- node[coordinate](origin){} ([shift={(0.5cm,-0.5cm)}]start plot -| end plot) node[right] {$\cdot$};
    \def\x{0.5}\def\y{4}\def\p{0.55}
    \coordinate (Ux) at (\x,{ln(\x)});
    \coordinate (Uy) at (\y,{ln(\y)});
    \coordinate (Up) at ({\p*\x+(1-\p)*\y},{ln(\p*\x+(1-\p)*\y)});
    \draw (Ux) -- coordinate[pos=1-\p] (Up-mid) (Uy);
    \path let \p1=(Up-mid), \n1={pow(e,\y1*0.03514)} in (28.4576*\n1,\y1) coordinate (Up-mid2);
    \draw[my grid] (Ux) |- node[below,font=\scriptsize]{$x$} (origin) |- node[left,font=\scriptsize]{$u(x)$} cycle;
   \draw[my grid] (Uy) |- node[below,font=\scriptsize]{$y$} (origin) |- node[left,font=\scriptsize]{$u(y)$} cycle;
    \draw[my grid] (Up) |- node[below, yshift=2.25pt, font=\scriptsize]{$px+(1-p)y$} (origin) |- node[left,font=\scriptsize]{$u(px+(1-p)y)$} cycle;
    \draw[my grid] (Up-mid) |- (origin) |- node[left,font=\scriptsize]{$pu(x)+(1-p)u(y)$} cycle;
    \draw[my grid] (Up-mid) -- (Up-mid2);
    \end{tikzpicture}}
    \end{adjustbox}
    \caption{Risk Aversion}\label{RA}
    \end{figure}
\end{minipage}
\hfill
\begin{minipage}[b]{0.49\textwidth}
    \begin{figure}[H]
    \begin{adjustbox}{width=1\textwidth}{
    \begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0.1:5.5}, my grid/.style={densely dotted,opacity=0.5, every node/.style={black,opacity=1}}, my axis/.style={latex-latex}]
    \draw[my plot, color=black] (0,0) plot (\x,{(\x)});
    \coordinate (start plot) at (0,{(0)});
    \coordinate (end plot) at (5.5,{(5.5)});
    \draw[my axis] ([shift={(-0.5cm,0.5cm)}]start plot |- end plot) node[above] {$u(\cdot)$} |- node[coordinate](origin){} ([shift={(0.5cm,-0.5cm)}]start plot -| end plot) node[right] {$\cdot$};
    \def\x{0.5}\def\y{4}\def\p{0.55}
    \coordinate (Ux) at (\x,{(\x)});
    \coordinate (Uy) at (\y,{(\y)});
    \coordinate (Up) at ({\p*\x+(1-\p)*\y},{(\p*\x+(1-\p)*\y)});
    \draw (Ux) -- coordinate[pos=1-\p] (Up-mid) (Uy);
    \path let \p1=(Up-mid), \n1={pow(e,\y1*0.03514)} in (28.4576*\n1,\y1) coordinate (Up-mid2);
    \draw[my grid] (Ux) |- node[below,font=\scriptsize]{$x$} (origin) |- node[left,font=\scriptsize]{$u(x)$} cycle;
    \draw[my grid] (Uy) |- node[below,font=\scriptsize]{$y$} (origin) |- node[left,font=\scriptsize]{$u(y)$} cycle;
    \draw[my grid] (Up) |- node[below, yshift=2.25pt, font=\scriptsize]{$px+(1-p)y$} (origin) |- node[align=right,font=\scriptsize,xshift=-40pt]{$pu(x)+(1-p)u(y)=$\\$=u(px+(1-p)y)$} cycle;
    \end{tikzpicture}}
    \end{adjustbox}
    \caption{Risk Neutrality}\label{RN}
    \end{figure}
\end{minipage}
\end{figure}

\end{document}

The result is this one:

enter image description here

Though the result does not look bad, I would like the plots to be the exact same size; that is, the same length of the axes, same font size, and so on. However, with my code I get that the plot in the right-hand side is slightly larger that the one in the left. I want to avoid that. Does anybody know how to achieve that? I am afraid I may need to adjust the axes of the figures, but whatever I try, I deliberately fail. Thank you all for your help

gernot
  • 49,614
EoDmnFOr3q
  • 2,299

1 Answers1

2

First, remove all the nested figure, adjustbox and minipage stuff. Then scale down the larger plot using the xscale and yscale options of tikz (or scale up the smaller plot, if you prefer). For the x-axis, you can compute the factor since the domain is given; it is 5/5.5=0.90909.... For the y-axis, I chose the practical approach and just modified it until it looked the same. So we have to add

\begin{tikzpicture}[...,xscale=0.90909,yscale=0.75]

in the second plot.

The advantage of this kind of scaling over \adjustbox is that just the coordinate system is scaled but not fonts and line widths.

Regarding your second question of how to place two figures side by side, may I refer you to Two figures side by side, LaTeX figures side by side and many similar posts.

enter image description here

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0.1:5}, my grid/.style={densely dotted,opacity=0.5, every node/.style={black,opacity=1}}, my axis/.style={latex-latex}]
    \draw[my plot, color=black] (0,0) plot (\x,{ln(\x)});
    \coordinate (start plot) at (0.1,{ln(0.1)});
    \coordinate (end plot) at (5,{ln(5)});
    \draw[my axis] ([shift={(-0.5cm,0.5cm)}]start plot |- end plot) node[above] {$u(\cdot)$} |- node[coordinate](origin){} ([shift={(0.5cm,-0.5cm)}]start plot -| end plot) node[right] {$\cdot$};
    \def\x{0.5}\def\y{4}\def\p{0.55}
    \coordinate (Ux) at (\x,{ln(\x)});
    \coordinate (Uy) at (\y,{ln(\y)});
    \coordinate (Up) at ({\p*\x+(1-\p)*\y},{ln(\p*\x+(1-\p)*\y)});
    \draw (Ux) -- coordinate[pos=1-\p] (Up-mid) (Uy);
    \path let \p1=(Up-mid), \n1={pow(e,\y1*0.03514)} in (28.4576*\n1,\y1) coordinate (Up-mid2);
    \draw[my grid] (Ux) |- node[below,font=\scriptsize]{$x$} (origin) |- node[left,font=\scriptsize]{$u(x)$} cycle;
   \draw[my grid] (Uy) |- node[below,font=\scriptsize]{$y$} (origin) |- node[left,font=\scriptsize]{$u(y)$} cycle;
    \draw[my grid] (Up) |- node[below, yshift=2.25pt, font=\scriptsize]{$px+(1-p)y$} (origin) |- node[left,font=\scriptsize]{$u(px+(1-p)y)$} cycle;
    \draw[my grid] (Up-mid) |- (origin) |- node[left,font=\scriptsize]{$pu(x)+(1-p)u(y)$} cycle;
    \draw[my grid] (Up-mid) -- (Up-mid2);
    \end{tikzpicture}
\quad
    \begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0.1:5.5}, my grid/.style={densely dotted,opacity=0.5, every node/.style={black,opacity=1}}, my axis/.style={latex-latex},xscale=0.90909,yscale=0.75]
    \draw[my plot, color=black] (0,0) plot (\x,{(\x)});
    \coordinate (start plot) at (0,{(0)});
    \coordinate (end plot) at (5.5,{(5.5)});
    \draw[my axis] ([shift={(-0.5cm,0.5cm)}]start plot |- end plot) node[above] {$u(\cdot)$} |- node[coordinate](origin){} ([shift={(0.5cm,-0.5cm)}]start plot -| end plot) node[right] {$\cdot$};
    \def\x{0.5}\def\y{4}\def\p{0.55}
    \coordinate (Ux) at (\x,{(\x)});
    \coordinate (Uy) at (\y,{(\y)});
    \coordinate (Up) at ({\p*\x+(1-\p)*\y},{(\p*\x+(1-\p)*\y)});
    \draw (Ux) -- coordinate[pos=1-\p] (Up-mid) (Uy);
    \path let \p1=(Up-mid), \n1={pow(e,\y1*0.03514)} in (28.4576*\n1,\y1) coordinate (Up-mid2);
    \draw[my grid] (Ux) |- node[below,font=\scriptsize]{$x$} (origin) |- node[left,font=\scriptsize]{$u(x)$} cycle;
    \draw[my grid] (Uy) |- node[below,font=\scriptsize]{$y$} (origin) |- node[left,font=\scriptsize]{$u(y)$} cycle;
    \draw[my grid] (Up) |- node[below, yshift=2.25pt, font=\scriptsize]{$px+(1-p)y$} (origin) |- node[align=right,font=\scriptsize,xshift=-40pt]{$pu(x)+(1-p)u(y)=$\\$=u(px+(1-p)y)$} cycle;
    \end{tikzpicture}
\end{document}

Edit: Here is the wrapper to fit the plots, including captions, on an article page. I additionally use the trick to remove the width of the long labels using \llap{...} such that the labels stick into the margin and the other figure, respectively.

enter image description here

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{blindtext}
\begin{document}
\blindtext
\begin{figure}
\newcommand\commonscalefactor{0.64}%
\begin{minipage}[b]{\dimexpr0.5\textwidth-1em}
  \centering
    \begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0.1:5}, my grid/.style={densely dotted,opacity=0.5, every node/.style={black,opacity=1}}, my axis/.style={latex-latex},scale=\commonscalefactor]
    \draw[my plot, color=black] (0,0) plot (\x,{ln(\x)});
    \coordinate (start plot) at (0.1,{ln(0.1)});
    \coordinate (end plot) at (5,{ln(5)});
    \draw[my axis] ([shift={(-0.5cm,0.5cm)}]start plot |- end plot) node[above] {$u(\cdot)$} |- node[coordinate](origin){} ([shift={(0.5cm,-0.5cm)}]start plot -| end plot) node[right] {$\cdot$};
    \def\x{0.5}\def\y{4}\def\p{0.55}
    \coordinate (Ux) at (\x,{ln(\x)});
    \coordinate (Uy) at (\y,{ln(\y)});
    \coordinate (Up) at ({\p*\x+(1-\p)*\y},{ln(\p*\x+(1-\p)*\y)});
    \draw (Ux) -- coordinate[pos=1-\p] (Up-mid) (Uy);
    \path let \p1=(Up-mid), \n1={pow(e,\y1*0.03514)} in (28.4576*\n1,\y1) coordinate (Up-mid2);
    \draw[my grid] (Ux) |- node[below,font=\scriptsize]{$x$} (origin) |- node[left,font=\scriptsize]{$u(x)$} cycle;
   \draw[my grid] (Uy) |- node[below,font=\scriptsize]{$y$} (origin) |- node[left,font=\scriptsize]{$u(y)$} cycle;
    \draw[my grid] (Up) |- node[below, yshift=2.25pt, font=\scriptsize]{$px+(1-p)y$} (origin) |- node[left,font=\scriptsize]
       {\llap{$u(px+(1-p)y)$}} cycle;
    \draw[my grid] (Up-mid) |- (origin) |- node[left,font=\scriptsize]{\llap{$pu(x)+(1-p)u(y)$}} cycle;
    \draw[my grid] (Up-mid) -- (Up-mid2);
    \end{tikzpicture}\\
  \caption{Risk Aversion}\label{RA}
\end{minipage}%
\hspace{2em}%
\begin{minipage}[b]{\dimexpr0.5\textwidth-1em}
  \centering
    \begin{tikzpicture}[my plot/.style={thick, smooth, samples=100, domain=0.1:5.5}, my grid/.style={densely dotted,opacity=0.5, every node/.style={black,opacity=1}}, my axis/.style={latex-latex},xscale=0.90909*\commonscalefactor,yscale=0.75*\commonscalefactor]
    \draw[my plot, color=black] (0,0) plot (\x,{(\x)});
    \coordinate (start plot) at (0,{(0)});
    \coordinate (end plot) at (5.5,{(5.5)});
    \draw[my axis] ([shift={(-0.5cm,0.5cm)}]start plot |- end plot) node[above] {$u(\cdot)$} |- node[coordinate](origin){} ([shift={(0.5cm,-0.5cm)}]start plot -| end plot) node[right] {$\cdot$};
    \def\x{0.5}\def\y{4}\def\p{0.55}
    \coordinate (Ux) at (\x,{(\x)});
    \coordinate (Uy) at (\y,{(\y)});
    \coordinate (Up) at ({\p*\x+(1-\p)*\y},{(\p*\x+(1-\p)*\y)});
    \draw (Ux) -- coordinate[pos=1-\p] (Up-mid) (Uy);
    \path let \p1=(Up-mid), \n1={pow(e,\y1*0.03514)} in (28.4576*\n1,\y1) coordinate (Up-mid2);
    \draw[my grid] (Ux) |- node[below,font=\scriptsize]{$x$} (origin) |- node[left,font=\scriptsize]{$u(x)$} cycle;
    \draw[my grid] (Uy) |- node[below,font=\scriptsize]{$y$} (origin) |- node[left,font=\scriptsize]{$u(y)$} cycle;
    \draw[my grid] (Up) |- node[below, yshift=2.25pt, font=\scriptsize]{$px+(1-p)y$} (origin) |- node[align=right,font=\scriptsize,xshift=-0.5em]{\llap{$pu(x)+(1-p)u(y)=$}\\\llap{$=u(px+(1-p)y)$}} cycle;
    \end{tikzpicture}\\
    \caption{Risk Neutrality}\label{RN}
  \end{minipage}%
\end{figure}
\end{document}
gernot
  • 49,614
  • I am willing to accept your answer as the answer I was looking for. Nonetheless, this fits in the document you create because you define a tiny page margin os 2mm. However, I am working with standard article margins. Therefore, the figures do not currently fit side by side with the code you provided: they are side by side but they exceed the line width. Also, I am numbering the figures and so on; so I need to keep the figure stuff. When adding the figure stuff, the second figure jumps below the first one. Maybe you could slightly change your answer to accommodate this. Thanks a lot. – EoDmnFOr3q Dec 03 '16 at 14:40
  • 1
    @Héctor I have added a note on how to put the figures side by side with captions. In one previous posting they are handled as separate figures, in the other one as subfigures of a common figure. Regarding size: Nothing prevents you from additionally scaling both figures by the same factor. If you add scale=0.5 in the first figure and multiply the scaling factors of the second one also by 0.5, they will only take half the space. – gernot Dec 03 '16 at 14:44
  • Marked as accepted. Your code and your comments were enough to get exactly the desired output. Thank you. – EoDmnFOr3q Dec 03 '16 at 15:21
  • 1
    @Héctor I've added a complete version. The \llap command may be useful to handle the long labels. – gernot Dec 03 '16 at 15:27