0

I am trying to align a table and a pgfplots graph using minipage. I tried to measure the table height and set it to the graph but they still are out of alignment. Ideally, the captions should be at the same height and the table and pgfplots should have the same dimensions. On top of that, there's a bad box that I need to eliminate.

Code:

\documentclass[a4paper,12pt,twoside]{article}
\usepackage{geometry}
 \geometry{
 inner=30mm,
 top=30mm,
 outer=20mm,
 bottom=20mm
 }
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{caption}
\usepackage{pgfplots}
\usepackage[no-math]{fontspec}
\setmainfont{TeX Gyre Termes}
\usepackage{unicode-math}
\setmathfont{TeX Gyre Termes Math}
\usepackage[cal=pxtx]{mathalpha}
\setmathfont[range={\mdblkcircle,\mdblksquare,\mdblkdiamond,\blacktriangle,\blacktriangledown,\smallblacktriangleleft,\smallblacktriangleright,\bigstar,\maltese}]{STIX Two Math}
\pgfplotsset{compat=newest}

\newsavebox{\tablebox} \newlength{\tableheight} \newenvironment{resizedtabular}[1] {\begin{lrbox}{\tablebox}\begin{tabular}{#1}} {\end{tabular}\end{lrbox}% \sbox{\tablebox}{\resizebox{\textwidth}{!}{\usebox{\tablebox}}}% \global\tableheight=\ht\tablebox \global\advance\tableheight\dp\tablebox \usebox{\tablebox}}

\newsavebox{\measuredSize} \newcommand{\resizeToWidth}[2]{% \pgfmathsetmacro{\pgfplotswidth}{#2}% \begin{lrbox}{\measuredSize}#1\end{lrbox}% \pgfmathsetmacro{\pgfplotswidth}{2*\pgfplotswidth-\wd\measuredSize}% #1% }

\newcommand{\inputPlot}{ \begin{tikzpicture} \begin{axis}[xmin=90,xmax=610,ymin=0,ymax=1.2,tick pos=left,height=\tableheight,width=\pgfplotswidth,xlabel=XX,ylabel=XX] \node[above] at (129.746, 0.10363) {$\mdblkcircle$}; \node[above] at (161.767, 0.13399) {$\mdblksquare$}; \node[above] at (194.096, 0.05136) {$\mdblkdiamond$}; \node[above] at (316.332, 0.06316) {$\blacktriangle$}; \node[above] at (329.88, 0.07794) {$\blacktriangledown$}; \node[above] at (376.988, 1) {$\smallblacktriangleleft$}; \node[above] at (429.946, 0.03925) {$\smallblacktriangleright$}; \node[above] at (468.742, 0.07811) {$\bigstar$}; \node[above] at (592.825, 0.04339) {$\maltese$}; \end{axis} \end{tikzpicture}}

\begin{document} \begin{table} \begin{minipage}[c]{0.49\textwidth} \centering \begin{resizedtabular}{@{}ccccc@{}} \toprule \multirow{2}{*}{XX} & \multicolumn{4}{c}{XX} \ \cmidrule(l){2-5} & XX & XX & XX & XX \ \cmidrule(r){1-1} $\mdblkcircle$ & 130 & 130 & 130 & 130 \ $\mdblksquare$ & 162 & 162 & 162 & 163 \ $\mdblkdiamond$ & 194 & 194 & 192 & 195 \ $\blacktriangle$ & 316 & 318 & 317 & 318 \ $\blacktriangledown$ & 330 & 330 & 328 & 331 \ $\smallblacktriangleleft$ & 377 & 377 & 377 & 379 \ $\smallblacktriangleright$ & 430 & 430 & 429 & 432 \ $\bigstar$ & 469 & $\dagger$ & 469 & 471 \ $\maltese$ & 593 & 591 & 591 & 595 \ \bottomrule \end{resizedtabular} \caption{Some Caption.} \end{minipage}% \hfill \begin{minipage}[c]{0.49\textwidth} \resizeToWidth{\inputPlot}{\textwidth} \captionof{figure}{Some Caption.} \end{minipage} \end{table} \end{document}

Any help would be appreciated.

Rodrigo
  • 567
  • 1
    To align the captions, just use \begin{minipage}[b] instead of [c] for both your minipage environments. On the scaling part: Either scale to a width, or to a height. Doing both gives a distorted font. The results look good, imho, just with using [b] instead of [c] as suggested earlier. – Skillmon Nov 09 '20 at 22:11

1 Answers1

1

I propose a solution based on the xcoffin package. The code was added at the end of the OP's code for easier comparison.

I put the four elements: plot, table and respective captions, without further handling, in xcoffins, boxes with handles which allow easy alignment with each other. I also added an \fbox around the plot to make it easy to compare widths and heights. The color rulers show the position of the captions, centered and aligned as requested.

Slightly more extensive explanations about applying xcoffin can be found in previous questions and answers to, in some way, similar problems.

Create a box using the tcolorbox package or any other? (image)

Leave space for three logos on the title page

I need a rather complex layout for a scholarly edition and don't know where to start

\documentclass[a4paper,12pt,twoside]{article}
\usepackage{geometry}
 \geometry{
 inner=30mm,
 top=30mm,
 outer=20mm,
 bottom=20mm
 }
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{caption}
\usepackage{pgfplots}
\usepackage[no-math]{fontspec}
\setmainfont{TeX Gyre Termes}
\usepackage{unicode-math}
\setmathfont{TeX Gyre Termes Math}
\usepackage[cal=pxtx]{mathalpha}
\setmathfont[range={\mdblkcircle,\mdblksquare,\mdblkdiamond,\blacktriangle,\blacktriangledown,\smallblacktriangleleft,\smallblacktriangleright,\bigstar,\maltese}]{STIX Two Math}
\pgfplotsset{compat=newest}

\newsavebox{\tablebox} \newlength{\tableheight} \newenvironment{resizedtabular}[1] {\begin{lrbox}{\tablebox}\begin{tabular}{#1}} {\end{tabular}\end{lrbox}% \sbox{\tablebox}{\resizebox{\textwidth}{!}{\usebox{\tablebox}}}% \global\tableheight=\ht\tablebox \global\advance\tableheight\dp\tablebox \usebox{\tablebox}}

\newsavebox{\measuredSize} \newcommand{\resizeToWidth}[2]{% \pgfmathsetmacro{\pgfplotswidth}{#2}% \begin{lrbox}{\measuredSize}#1\end{lrbox}% \pgfmathsetmacro{\pgfplotswidth}{2*\pgfplotswidth-\wd\measuredSize}% #1% }

\newcommand{\inputPlot}{% \begin{tikzpicture} \begin{axis}[xmin=90,xmax=610,ymin=0,ymax=1.2,tick pos=left,height=\tableheight,width=\pgfplotswidth,xlabel=XX,ylabel=XX] \node[above] at (129.746, 0.10363) {$\mdblkcircle$}; \node[above] at (161.767, 0.13399) {$\mdblksquare$}; \node[above] at (194.096, 0.05136) {$\mdblkdiamond$}; \node[above] at (316.332, 0.06316) {$\blacktriangle$}; \node[above] at (329.88, 0.07794) {$\blacktriangledown$}; \node[above] at (376.988, 1) {$\smallblacktriangleleft$}; \node[above] at (429.946, 0.03925) {$\smallblacktriangleright$}; \node[above] at (468.742, 0.07811) {$\bigstar$}; \node[above] at (592.825, 0.04339) {$\maltese$}; \end{axis} \end{tikzpicture}}

\usepackage{xcoffins}

\begin{document} \begin{table} \begin{minipage}[c]{0.49\textwidth} \centering \begin{resizedtabular}{@{}ccccc@{}} \toprule \multirow{2}{*}{XX} & \multicolumn{4}{c}{XX} \ \cmidrule(l){2-5} & XX & XX & XX & XX \ \cmidrule(r){1-1} $\mdblkcircle$ & 130 & 130 & 130 & 130 \ $\mdblksquare$ & 162 & 162 & 162 & 163 \ $\mdblkdiamond$ & 194 & 194 & 192 & 195 \ $\blacktriangle$ & 316 & 318 & 317 & 318 \ $\blacktriangledown$ & 330 & 330 & 328 & 331 \ $\smallblacktriangleleft$ & 377 & 377 & 377 & 379 \ $\smallblacktriangleright$ & 430 & 430 & 429 & 432 \ $\bigstar$ & 469 & $\dagger$ & 469 & 471 \ $\maltese$ & 593 & 591 & 591 & 595 \ \bottomrule \end{resizedtabular} \caption{Some Caption.} \end{minipage}% \hfill \begin{minipage}[c]{0.49\textwidth} \resizeToWidth{\inputPlot}{\textwidth} \captionof{figure}{Some Caption.} \end{minipage} \end{table}

%%%%%%%%**************** xcoffin solution

\textbf{The xcoffin solution}

\NewCoffin\Framex \NewCoffin\Tablex \NewCoffin\CaptionTablex \NewCoffin\Plotx \NewCoffin\CaptionPlotx

\NewCoffin\Hrule %for degugging \SetHorizontalCoffin\Hrule{\color{red}\rule{\textwidth}{0.2pt}} \NewCoffin\Vrule %for degugging \SetHorizontalCoffin\Vrule{\color{green}\rule{0.2pt}{0.5\textheight}}

\SetHorizontalCoffin\Tablex{%
\begin{minipage}[c]{0.49\textwidth} \begin{resizedtabular}{@{}ccccc@{}} \toprule \multirow{2}{*}{XX} & \multicolumn{4}{c}{XX} \ \cmidrule(l){2-5} & XX & XX & XX & XX \ \cmidrule(r){1-1} $\mdblkcircle$ & 130 & 130 & 130 & 130 \ $\mdblksquare$ & 162 & 162 & 162 & 163 \ $\mdblkdiamond$ & 194 & 194 & 192 & 195 \ $\blacktriangle$ & 316 & 318 & 317 & 318 \ $\blacktriangledown$ & 330 & 330 & 328 & 331 \ $\smallblacktriangleleft$ & 377 & 377 & 377 & 379 \ $\smallblacktriangleright$ & 430 & 430 & 429 & 432 \ $\bigstar$ & 469 & $\dagger$ & 469 & 471 \ $\maltese$ & 593 & 591 & 591 & 595 \ \bottomrule \end{resizedtabular} \end{minipage}
}

\SetVerticalCoffin\CaptionTablex{0.49\textwidth}{\captionof{table}{Some Caption.}}

\SetHorizontalCoffin\Plotx{% \fbox{\begin{minipage}[c]{0.49\textwidth} \resizeToWidth{\inputPlot}{\textwidth} \end{minipage}} }

\SetVerticalCoffin\CaptionPlotx{0.49\textwidth}{\captionof{figure}{Some Caption.}}

\ResizeCoffin\Plotx{0.49\textwidth}{\CoffinTotalHeight\Tablex} % adjust plot height to table height

\JoinCoffins\Framex[hc,vc]\Tablex[l,t] \JoinCoffins\Framex[\Tablex-hc, \Tablex-b]\CaptionTablexhc,t %down shift of 2ex

\JoinCoffins\Framex[ \Tablex-r, \Tablex-t]\Plotxl,t %right shift of 10pt \JoinCoffins\Framex[\Plotx-hc, \Plotx-b]\CaptionPlotxhc,t %down shift of 2ex

%%------------------------------- checks \JoinCoffins\Framex[hc,vc]\Hrule[l,t] % comment after cheking alignment \JoinCoffins\Framex[\Tablex-l,\Tablex-b]\Hrule[l,t] % comment after cheking alignment \JoinCoffins*\Framex[\CaptionTablex-l,\CaptionTablex-vc]\Hrule[l,t] % comment after cheking alignment

\JoinCoffins\Framex[\Plotx-hc,\Plotx-t]\Vrule[r,t] % comment after cheking alignment \JoinCoffins\Framex[\Tablex-hc,\Tablex-t]\Vrule[r,t] % comment after cheking alignment %%-------------------------------------

\noindent\TypesetCoffin\Framex \end{document}

output

The only improvement so far is the alignment of the captions. (No bad box detected from my side, compiling the above code). But we can do better.

The natural width of the table is less than 0.49\textwidth. So, by resizing that length you are also enlarging the font size, and consequently it does not match those of the plot. With some little changes you can get a more harmonic looking output (\fbox added) :

\documentclass[a4paper,12pt,twoside]{article}  %only coffin
\usepackage{geometry}
 \geometry{
 inner=30mm,
 top=30mm,
 outer=20mm,
 bottom=20mm
 }
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{caption}
\usepackage{pgfplots}
\usepackage[no-math]{fontspec}
\setmainfont{TeX Gyre Termes}
\usepackage{unicode-math}
\setmathfont{TeX Gyre Termes Math}
\usepackage[cal=pxtx]{mathalpha}
\setmathfont[range={\mdblkcircle,\mdblksquare,\mdblkdiamond,\blacktriangle,\blacktriangledown,\smallblacktriangleleft,\smallblacktriangleright,\bigstar,\maltese}]{STIX Two Math}
\pgfplotsset{compat=newest}

\usepackage{xcoffins} %<<<< added

\usepackage{calc} %<<<< added \usepackage{kantlipsum}%<<<< added

\begin{document}

\textbf{The xcoffin solution v2}

\NewCoffin\Framex \NewCoffin\Tablex \NewCoffin\CaptionTablex \NewCoffin\Plotx \NewCoffin\CaptionPlotx

\NewCoffin\Hrule %for degugging \SetHorizontalCoffin\Hrule{\color{red}\rule{\textwidth}{0.2pt}} \NewCoffin\Vrule %for degugging \SetHorizontalCoffin\Vrule{\color{green}\rule{0.2pt}{0.5\textheight}}

\SetHorizontalCoffin\Tablex{%
\begin{tabular}{@{}ccccc@{}} \toprule \multirow{2}{*}{XX} & \multicolumn{4}{c}{XX} \ \cmidrule(l){2-5} & XX & XX & XX & XX \ \cmidrule(r){1-1} $\mdblkcircle$ & 130 & 130 & 130 & 130 \ $\mdblksquare$ & 162 & 162 & 162 & 163 \ $\mdblkdiamond$ & 194 & 194 & 192 & 195 \ $\blacktriangle$ & 316 & 318 & 317 & 318 \ $\blacktriangledown$ & 330 & 330 & 328 & 331 \ $\smallblacktriangleleft$ & 377 & 377 & 377 & 379 \ $\smallblacktriangleright$ & 430 & 430 & 429 & 432 \ $\bigstar$ & 469 & $\dagger$ & 469 & 471 \ $\maltese$ & 593 & 591 & 591 & 595 \ \bottomrule \end{tabular} }

\newlength{\TableH} \setlength{\TableH}{\CoffinTotalHeight\Tablex}

\SetVerticalCoffin\CaptionTablex{\CoffinWidth\Tablex}{\captionof{table}{Some Caption.\label{tab1}}}

\SetHorizontalCoffin\Plotx{% \fbox{%
\begin{tikzpicture} \begin{axis}[xmin=90,xmax=610,ymin=0,ymax=1.2,tick pos=left,height=\TableH,width=0.6\textwidth,xlabel=XX,ylabel=XX] \node[above] at (129.746, 0.10363) {$\mdblkcircle$}; \node[above] at (161.767, 0.13399) {$\mdblksquare$}; \node[above] at (194.096, 0.05136) {$\mdblkdiamond$}; \node[above] at (316.332, 0.06316) {$\blacktriangle$}; \node[above] at (329.88, 0.07794) {$\blacktriangledown$}; \node[above] at (376.988, 1) {$\smallblacktriangleleft$}; \node[above] at (429.946, 0.03925) {$\smallblacktriangleright$}; \node[above] at (468.742, 0.07811) {$\bigstar$}; \node[above] at (592.825, 0.04339) {$\maltese$}; \end{axis} \end{tikzpicture} } }

\ResizeCoffin\Plotx{0.6\textwidth}{\TableH}

\SetVerticalCoffin\CaptionPlotx{\CoffinWidth\Plotx}{\captionof{figure}{Some Caption.\label{fig1}}}

\JoinCoffins\Framex[hc,vc]\Tablex[l,t] \JoinCoffins\Framex[\Tablex-hc, \Tablex-b]\CaptionTablexhc,t %down shift of 2ex

\JoinCoffins\Framex[ \Tablex-r, \Tablex-t]\Plotxl,t %right shift of 20pt \JoinCoffins\Framex[\Plotx-hc, \Plotx-b]\CaptionPlotxhc,t %down shift of 2ex

%%------------------------------- checks \JoinCoffins\Framex[hc,vc]\Hrule[l,t] % comment after cheking alignment \JoinCoffins\Framex[\Tablex-l,\Tablex-b]\Hrule[l,t] % comment after cheking alignment

\JoinCoffins*\Framex[\CaptionTablex-l,\CaptionTablex-vc]\Hrule[l,t]% comment after cheking alignment

\JoinCoffins\Framex[\Plotx-hc,\Plotx-t]\Vrule[r,t] % comment after cheking alignment \JoinCoffins\Framex[\Tablex-hc,\Tablex-t]\Vrule[r,t] % comment after cheking alignment %%-------------------------------------

\noindent\TypesetCoffin\Framex

\newlength{\makeVspace} \setlength{\makeVspace}{\CoffinTotalHeight\Tablex+\CoffinTotalHeight\CaptionTablex }
\vspace{\makeVspace}
\kant[1]

As shown in Table \ref{tab1} and Figure \ref{fig1}.

\end{document}

g3New

Please note that now the xcoffin code is now much cleaner, since only the the environments tabular and tikzpicture need to be retained inside the xcoffins. The height of the plot is set to be equal to the table, like you did before.

UPDATE As requested I added some text after the picture. This is the way to do it:

\noindent\TypesetCoffin\Framex will put the assemblage in the next insertion point after the title "The xcoffin solution v2", as can be seen.

The xcoffin \Framex has zero dimension and serves as a collector of the others xcoffins

In this example I chose to use \JoinCoffin* instead of \JoinCoffin, thus the size of \Framex will remain zero during the assembly. That means that if you want to put some text or other material after the figure you must add some vertical space.

For example adding, at minimum, the height of the table plus the height of the caption.

The added code will then be (packages kantlipsum and calc also added)

\newlength{\makeVspace}
\setlength{\makeVspace}{\CoffinTotalHeight\Tablex+\CoffinTotalHeight\CaptionTablex} 
\vspace{\makeVspace}

\kant[1]

Simon Dispa
  • 39,141
  • This looks awesome. However, if I try to type anything after that figure, it shows up on top of it. How can I fix that? – Rodrigo Nov 11 '20 at 03:29
  • I updated the code and added some explanation on how to add material after the figure created with xcoffin. – Simon Dispa Nov 11 '20 at 13:15
  • @Rodrigo Please see the updated answer. – Simon Dispa Nov 11 '20 at 13:16
  • There is still a problem. I can't make make the references for the Figure and the Table to work. For instance, if I add: "As shown in Table \ref{tab1} and Figure \ref{fig1}", the numbers don't show up. – Rodrigo Nov 11 '20 at 16:29
  • Use \SetVerticalCoffin\CaptionTablex{\CoffinWidth\Tablex}{\captionof{table}{Some Caption.\label{tab1}}} and \SetVerticalCoffin\CaptionPlotx{\CoffinWidth\Plotx}{\captionof{figure}{Some Caption.\label{fig1}}}. You did not include references in the MWE. – Simon Dispa Nov 11 '20 at 17:04
  • Thank for your feedback!! Helps to refine the answer. – Simon Dispa Nov 11 '20 at 17:11
  • Thank you. Now it's perfect :) – Rodrigo Nov 11 '20 at 18:37