浮动的自动宽度

浮动的自动宽度

我需要自动计算表格和图形的宽度;包括包含子图的图形的总宽度。我还希望代码包含一个最小宽度参数,以便在表格或图形较窄时使用。我感谢成员提供的任何帮助。

我在下面添加了一个最小工作示例。Martin,我想看看你提到的代码。感谢所有回复的人。

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{subfig}
\newlength{\floatwidth} % The code should determine this value
\newenvironment{mytext}%          environment name
    {\newline\hspace*{-.15in}\begin{minipage}{\floatwidth}\textbf{mytext: }\begin{itshape}}% begin code
    {\end{itshape}\end{minipage}}
\begin{document}
\setlength{\floatwidth}{4.4in}
\begin{figure}[tbp]
    \begin{center}
        \caption{Figure Caption}
        \subfloat[First figure]{\label{fig:firstfig}\includegraphics{figure1}} \qquad
        \subfloat[Second figure]{\label{fig:secondfig}\includegraphics{figure2}}
        \begin{mytext}
            Acceptable! The text in this environment is left justified on the left side of the left-most figure. If the text is long enough, it should wrap at the right side of the right-most figure.
        \end{mytext}
    \end{center}
\end{figure}
\end{document}

答案1

要测量表格或图像的宽度,您需要使用保存框(例如\newsavebox\mysavebox)并将其存储在其中,使用\sbox\mysavebox{...})\savebox[<width>}{\mysavebox}{...}{lrbox}{\mysavebox}环境。然后您可以使用获取宽度\wd\mysavebox。对于较大的事物(例如多个子图),您需要将它们分组到一个框中。

adjustbox包提供了一个{adjustbox}{<key=value>}环境,将其内容存储在这样的保存框中,并提供\width可用作键的一部分的宽度。有一个min width=<length>键会将内容缩放到该宽度(如果宽度较小)。这将适用于子图,只要它们不介意被放入一个水平框中。否则,您可以minipage=<length>先使用键将它们放入(垂直)中minipage。但是,这将缩放此环境中的所有内容,包括标题文本。如果您向我们提供有关您的特定应用程序的更精确信息,我可以为您提供一些示例代码,最好以最小工作示例(MWE)

答案2

可以使用 PDF 标记来精确定位(并引用)页面上元素的确切位置,方法是zref包裹激活模块后savepos

\usepackage[savepos]{zref}% http://ctan.org/pkg/zref

现在您可以使用 指定一个标记。可以分别使用和提取\zsavepos{<label>}标记的 x,y 坐标。<label>\zposx{<label>}\zposy{<label>}

在以下最小示例中,宏\leftpos{<label>}用于图形/表格的最左侧点,而\rightpos{<label>}用于最右侧点。然后,使用提供的来存储 中的和之间\getHdist{<length>}[<left-label>]{<right-label>}的距离。第二个参数是可选的,如果未指定,则默认为。这样做的原因是,人们通常可能对图形/表格中的左侧和右侧位置使用相同的标签。<left-label><right-label><length>[<left-label>]<right-label>

在此处输入图片描述

\documentclass{article}
\usepackage{printlen}% http://ctan.org/pkg/printlen
\usepackage[demo]{graphicx}% http://ctan.org/pkg/graphicx
\usepackage[savepos]{zref}% http://ctan.org/pkg/zref
\usepackage{xparse}% http://ctan.org/pkg/xparse
\makeatletter
\newcommand{\leftpos}[1]{\mbox{}\zsavepos{leftpos@#1}}%
\newcommand{\rightpos}[1]{\zsavepos{rightpos@#1}\mbox{}}%
\NewDocumentCommand{\getHdist}{m o m}{%
  \IfNoValueTF{#2}%
    {\setlength{#1}{\dimexpr\zposx{rightpos@#3}sp-\zposx{leftpos@#3}sp\relax}}%
    {\setlength{#1}{\dimexpr\zposx{rightpos@#3}sp-\zposx{leftpos@#2}sp\relax}}%
}
\makeatother
\newlength{\figwidth} \newlength{\tblwidth}
\begin{document}
\uselengthunit{pt}% Print lengths in pt measurement

\begin{figure}[t]
  \centering
  \leftpos{fig:myfig}\includegraphics{figure1} \qquad \includegraphics{figure2}\rightpos{fig:myfig}

  \rule{323pt}{1pt}
  \caption{This is a figure caption} \label{fig:myfig}
\end{figure}

\begin{table}[t]
  \centering
  \leftpos{tbl:mytbl}%
  \begin{tabular}{@{}p{50pt}@{}p{60pt}@{}}
    Some & thing \\
    in & a \\
    tabular & that \\
    is & exactly \\
    50pt & + 60pt \\
    wide & !
  \end{tabular}%
  \rightpos{tbl:mytbl}

  \rule{110pt}{1pt}
  \caption{This is a table caption} \label{tbl:mytbl}
\end{table}

\getHdist{\figwidth}{fig:myfig} \getHdist{\tblwidth}{tbl:mytbl}

The width of Figure~\ref{fig:myfig} is \printlength{\figwidth}. The width of Table~\ref{tbl:mytbl} is \printlength{\tblwidth}.

\end{document}

在上面的例子中,图形和表格下方的水平规则被硬编码为固定宽度的规则(323pt对于图形和110pt表格),仅作为参考;以显示正确的长度计算。

graphicx已加载demo模拟正确使用图形的选项。它将每个图形(无论是否存在图像)排版为 150pt x 100pt 黑色矩形/框。demo在最终文档中删除该选项。printlen还加载了 TeX 长度的打印功能\printlength{<len>}(使用 指定默认长度单位\uselengthunit{<unit>};我选择了pt)。最后,xparse已加载以提供用于生成文档命令/环境的简便界面。

相关内容