TransWikia.com

Aligning a table and a pgfplots graph

TeX - LaTeX Asked on December 18, 2020

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}}}%
  globaltableheight=httablebox
  globaladvancetableheightdptablebox
  usebox{tablebox}}

newsavebox{measuredSize}
newcommand{resizeToWidth}[2]{%
    pgfmathsetmacro{pgfplotswidth}{#2}%
    begin{lrbox}{measuredSize}#1end{lrbox}%
    pgfmathsetmacro{pgfplotswidth}{2*pgfplotswidth-wdmeasuredSize}%
    #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.49textwidth}
        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.49textwidth}
        resizeToWidth{inputPlot}{textwidth}
        captionof{figure}{Some Caption.}
    end{minipage}
end{table}
end{document}

Any help would be appreciated.

One Answer

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}}}%
  globaltableheight=httablebox
  globaladvancetableheightdptablebox
  usebox{tablebox}}

newsavebox{measuredSize}
newcommand{resizeToWidth}[2]{%
    pgfmathsetmacro{pgfplotswidth}{#2}%
    begin{lrbox}{measuredSize}#1end{lrbox}%
    pgfmathsetmacro{pgfplotswidth}{2*pgfplotswidth-wdmeasuredSize}%
    #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.49textwidth}
        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.49textwidth}
        resizeToWidth{inputPlot}{textwidth}
        captionof{figure}{Some Caption.}
    end{minipage}
end{table}

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

textbf{The xcoffin solution}

NewCoffinFramex
NewCoffinTablex
NewCoffinCaptionTablex
NewCoffinPlotx
NewCoffinCaptionPlotx

NewCoffinHrule %for degugging
SetHorizontalCoffinHrule{color{red}rule{textwidth}{0.2pt}}
NewCoffinVrule %for degugging
SetHorizontalCoffinVrule{color{green}rule{0.2pt}{0.5textheight}}   

SetHorizontalCoffinTablex{%   
 begin{minipage}[c]{0.49textwidth}
        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}  
}

SetVerticalCoffinCaptionTablex{0.49textwidth}{captionof{table}{Some Caption.}}  

SetHorizontalCoffinPlotx{%
fbox{begin{minipage}[c]{0.49textwidth}
    resizeToWidth{inputPlot}{textwidth}
end{minipage}}
}   

SetVerticalCoffinCaptionPlotx{0.49textwidth}{captionof{figure}{Some Caption.}}

ResizeCoffinPlotx{0.49textwidth}{CoffinTotalHeightTablex} % adjust plot height to table height

JoinCoffins*Framex[hc,vc]Tablex[l,t]
JoinCoffins*Framex[Tablex-hc, Tablex-b]CaptionTablex[hc,t](0pt,-2ex) %down shift of 2ex

JoinCoffins*Framex[ Tablex-r, Tablex-t]Plotx[l,t](10pt,0pt)  %right shift of 10pt
JoinCoffins*Framex[Plotx-hc, Plotx-b]CaptionPlotx[hc,t](0pt,-2ex) %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
%%-------------------------------------

noindentTypesetCoffinFramex
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.49textwidth. 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}

NewCoffinFramex
NewCoffinTablex
NewCoffinCaptionTablex
NewCoffinPlotx
NewCoffinCaptionPlotx

NewCoffinHrule %for degugging
SetHorizontalCoffinHrule{color{red}rule{textwidth}{0.2pt}}
NewCoffinVrule %for degugging
SetHorizontalCoffinVrule{color{green}rule{0.2pt}{0.5textheight}}
    
SetHorizontalCoffinTablex{%           
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}{CoffinTotalHeightTablex}

SetVerticalCoffinCaptionTablex{CoffinWidthTablex}{captionof{table}{Some Caption.label{tab1}}} 

SetHorizontalCoffinPlotx{%
    fbox{%     
        begin{tikzpicture}
    begin{axis}[xmin=90,xmax=610,ymin=0,ymax=1.2,tick pos=left,height=TableH,width=0.6textwidth,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}
}
}

ResizeCoffinPlotx{0.6textwidth}{TableH}
    
SetVerticalCoffinCaptionPlotx{CoffinWidthPlotx}{captionof{figure}{Some Caption.label{fig1}}}  

JoinCoffins*Framex[hc,vc]Tablex[l,t]
JoinCoffins*Framex[Tablex-hc, Tablex-b]CaptionTablex[hc,t](0pt,-2ex) %down shift of 2ex    

JoinCoffins*Framex[ Tablex-r, Tablex-t]Plotx[l,t](20pt,0pt)  %right shift of 20pt
JoinCoffins*Framex[Plotx-hc, Plotx-b]CaptionPlotx[hc,t](0pt,-2ex) %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
%%-------------------------------------

noindentTypesetCoffinFramex  

newlength{makeVspace}
setlength{makeVspace}{CoffinTotalHeightTablex+CoffinTotalHeightCaptionTablex }    
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:

noindentTypesetCoffinFramex 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}{CoffinTotalHeightTablex+CoffinTotalHeightCaptionTablex} 
vspace{makeVspace}

kant[1]    

Correct answer by Simon Dispa on December 18, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP