Adjustbox 不会减少表格和 tikzpicture 的宽度

Adjustbox 不会减少表格和 tikzpicture 的宽度

我正在重新设计大学课程的讲义,因为学生们想在平板电脑、电子书阅读器等设备上阅读这些笔记。所以我编写了一个 Makefile,其中包含针对不同页面大小的不同目标。我唯一的问题是一些 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}

在此处输入图片描述

相关内容