对齐表格和 pgfplots 图表

对齐表格和 pgfplots 图表

我正在尝试使用 minipage 对齐表格和 pgfplots 图表。我尝试测量表格高度并将其设置为图表,但它们仍然未对齐。理想情况下,标题应位于同一高度,表格和 pgfplots 应具有相同的尺寸。除此之外,还有一个坏框需要消除。

代码:

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

任何帮助,将不胜感激。

答案1

我提出了一个基于该xcoffin软件包的解决方案。为了便于比较,我将代码添加到了 OP 代码的末尾。

我将四个元素:图、表和相应的标题(无需进一步处理)放在 xcoffins 中,这些框带有手柄,可以轻松相互对齐。我还在\fbox图周围添加了一个,以便于比较宽度和高度。颜色标尺显示标题的位置,按要求居中和对齐。

关于应用的更详细的解释xcoffin可以在以前的问题和对类似问题的回答中找到。

使用 tcolorbox 包或任何其他包创建一个框?(图像)

在标题页上留出三个徽标的空间

我需要一个相当复杂的学术版布局,但不知道从哪里开始

\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]\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
%%-------------------------------------

\noindent\TypesetCoffin\Framex
\end{document}

输出

到目前为止,唯一的改进是字幕的对齐。(编译上述代码时,我这边没有检测到坏框)。但我们可以做得更好。

表格的自然宽度小于0.49\textwidth。因此,通过调整长度,您也会扩大字体大小,因此它与图表的字体不匹配。通过一些小的更改,您可以获得看起来更和谐的输出(\fbox已添加):

\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]\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
%%-------------------------------------

\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

请注意,现在xcoffin代码更加简洁了,因为只需要将环境tabulartikzpicture保留在 xcoffins 中。绘图的高度设置为与表格的高度相等,就像您之前所做的那样。

更新根据要求,我在图片后添加了一些文字。操作方法如下:

\noindent\TypesetCoffin\Framex将会把组合件放在标题“xcoffin 解决方案 v2”后的下一个插入点中,如下图所示。

xcoffin 的\Framex维度为零,用于收集其他 xcoffin

在这个例子中,我选择使用\JoinCoffin*而不是\JoinCoffin,因此 的大小\Framex在组装过程中将保持为零。这意味着如果您想在图形后放置一些文本或其他材料,则必须添加一些垂直空间。

例如,至少添加表格的高度加上标题的高度。

然后添加的代码将是(包kantlipsumcalc添加)

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

\kant[1]    

相关内容