我正在嘗試使用 minipage 對齊表格和 pgfplots 圖表。我嘗試測量桌子高度並將其設置為圖表,但它們仍然不對齊。理想情況下,標題應具有相同的高度,並且表格和 pgfplot 應該具有相同的尺寸。最重要的是,我需要消除一個壞盒子。
代碼:
\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程式碼的末尾,以便於比較。
我將四個元素:繪圖、表格和各自的標題,無需進一步處理,放入帶有手柄的盒子中,可以輕鬆地相互對齊。我還在\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}
請注意,現在xcoffin
程式碼更清晰,因為只有環境tabular
和tikzpicture
需要保留在 xcoffins 內。繪圖的高度設定為等於表格,就像您之前所做的那樣。
更新按照要求,我在圖片後面添加了一些文字。這是這樣做的方法:
\noindent\TypesetCoffin\Framex
會將組合放在標題「The xcoffin Solution v2」之後的下一個插入點中,如圖所示。
xcoffin\Framex
具有零維度並充當其他 xcoffin 的收集器
在此範例中,我選擇使用\JoinCoffin*
代替,因此在組裝過程中\JoinCoffin
的大小將保持為零。\Framex
這意味著,如果您想在圖形後面放置一些文字或其他材料,則必須添加一些垂直空間。
例如,至少要新增表格的高度加上標題的高度。
添加的代碼將是(包kantlipsum
並calc
添加)
\newlength{\makeVspace}
\setlength{\makeVspace}{\CoffinTotalHeight\Tablex+\CoffinTotalHeight\CaptionTablex}
\vspace{\makeVspace}
\kant[1]