![Adjustbox 不會減少表格和 tikzpicture 寬度](https://rvso.com/image/281580/Adjustbox%20%E4%B8%8D%E6%9C%83%E6%B8%9B%E5%B0%91%E8%A1%A8%E6%A0%BC%E5%92%8C%20tikzpicture%20%E5%AF%AC%E5%BA%A6.png)
我正在重新設計大學課程的講義,因為學生想在平板電腦、電子書閱讀器等上閱讀講義。我遇到的唯一問題是一些 PGF/TikZ 圖形和一些表格太大,無法適應小紙張尺寸。
我想用adjustbox
如果 PGF/TikZ 中繪製的表格和圖形太大而無法適應定義的紙張尺寸,則縮小它們的大小。然而,它並沒有真正起作用。
\documentclass[10pt]{article}
% set page size with geometry
\usepackage[nohead,%
nofoot,%
nomarginpar,%
paperwidth=106.68mm,%
paperheight=142.24mm,%
tmargin=2.5mm,%
rmargin=2.5mm,%
bmargin=2.5mm,%
lmargin=2.5mm]{geometry}
\usepackage{float}
\usepackage{tikz}
\usepackage{adjustbox}
%define lengths for maximum figure and table width and height
\newlength{\maxtabfigwidth}
\newlength{\maxtabfigheight}
\setlength{\maxtabfigwidth}{\textwidth}
\setlength{\maxtabfigheight}{\textheight}
% decrease height a bit letting captions fit to one page
\addtolength{\maxtabfigheight}{-2.5em}
\pagestyle{empty}
\begin{document}
\section*{Example \#1}
The width of the table isn't reduced to \texttt{\textbackslash{}maxtabfigwidth}.
\begin{adjustbox}{center,%
max width={\maxtabfigwidth},%
max totalheight={\maxtabfigheight},%
captionbelow={A wide table},%
float={table}[h!]}
\begin{tabular}{p{6cm}p{6cm}}
\hline
wide & table \\\hline
\end{tabular}
\end{adjustbox}
\section*{Example \#2}
The width of the tikzpicture isn't reduced to \texttt{\textbackslash{}maxtabfigwidth}.
\begin{adjustbox}{center,%
max width={\maxtabfigwidth},%
max totalheight={\maxtabfigheight},%
captionbelow={A wide tikzpicture},%
float={figure}[H]}
\begin{tikzpicture}
\fill[black] (0cm, 0cm) -- (0cm, -3cm) -- (12cm, -3cm) -- (12cm, 0cm) -- cycle;
\end{tikzpicture}
\end{adjustbox}
\section*{Example \#3}
The height of the tikzpicture is reduced to\texttt{\textbackslash{}maxtabfigheight}, however it is not centered.
\begin{adjustbox}{center,%
max width={\maxtabfigwidth},%
max totalheight={\maxtabfigheight},%
captionbelow={A tall tikzpicture},%
float={figure}[H]}
\begin{tikzpicture}
\fill[black] (0cm, 0cm) -- (0cm, -15cm) -- (5cm, -15cm) -- (5cm, 0cm) -- cycle;
\end{tikzpicture}
\end{adjustbox}
\end{document}
所以問題是我錯過了什麼或做錯了什麼?如果表格和圖形不適合小紙張尺寸,是否有其他方法可以縮小表格和圖形?
答案1
有兩個問題。首先,您需要稍後計算\maxtabfigwidth
和的值\maxtabfigheight
,因為geometry
它的計算在開始文件中進行。
第二個問題是選項的順序adjustbox
很重要。特別是center
必須追隨max width
和max totalheight
。
\documentclass[10pt]{article}
% set page size with geometry
\usepackage[nohead,%
nofoot,%
nomarginpar,%
paperwidth=106.68mm,%
paperheight=142.24mm,%
tmargin=2.5mm,%
rmargin=2.5mm,%
bmargin=2.5mm,%
lmargin=2.5mm]{geometry}
\usepackage{float}
\usepackage{tikz}
\usepackage{adjustbox}
%define lengths for maximum figure and table width and height
\newlength{\maxtabfigwidth}
\newlength{\maxtabfigheight}
\AtBeginDocument{
\setlength{\maxtabfigwidth}{\textwidth}
\setlength{\maxtabfigheight}{\textheight}
% decrease height a bit letting captions fit to one page
\addtolength{\maxtabfigheight}{-2.5em}
}
\pagestyle{empty}
\begin{document}
\section*{Example \#1}
The width of the table isn't reduced to \texttt{\textbackslash{}maxtabfigwidth}.
\begin{adjustbox}{
max width=\maxtabfigwidth,
max totalheight=\maxtabfigheight,
center,
captionbelow={A wide table},
float={table}[h!],
}
\begin{tabular}{p{6cm}p{6cm}}
\hline
wide & table \\\hline
\end{tabular}
\end{adjustbox}
\section*{Example \#2}
The width of the tikzpicture isn't reduced to \texttt{\textbackslash{}maxtabfigwidth}.
\begin{adjustbox}{
max width=\maxtabfigwidth,
max totalheight=\maxtabfigheight,
center,
captionbelow={A wide tikzpicture},%
float={figure}[H],
}
\begin{tikzpicture}
\fill[black] (0cm, 0cm) -- (0cm, -3cm) -- (12cm, -3cm) -- (12cm, 0cm) -- cycle;
\end{tikzpicture}
\end{adjustbox}
\section*{Example \#3}
The height of the tikzpicture is reduced to\texttt{\textbackslash{}maxtabfigheight}, however
it is not centered.
\begin{adjustbox}{
max width=\maxtabfigwidth,
max totalheight=\maxtabfigheight,
center,
captionbelow={A tall tikzpicture},
float={figure}[H],
}
\begin{tikzpicture}
\fill[black] (0cm, 0cm) -- (0cm, -15cm) -- (5cm, -15cm) -- (5cm, 0cm) -- cycle;
\end{tikzpicture}
\end{adjustbox}
\end{document}