测量页面的一部分

测量页面的一部分

我正在尝试测量页面某部分的高度。

完整的标题页以及我要测量的页面部分都以红色标出,编译页面的代码显示在下方。是的,我必须一次性测量所有内容。:-(

https://i.stack.imgur.com/kXhDH.png

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\begin{document}
\thispagestyle{plain}

\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
\vskip 2ex
\textcolor{lightgray}{\hrule height .5pt}
\vskip 6ex
\Huge{A title in fontsize Huge}
\vskip .4ex
\large{Doe, J. (John)}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
  \vskip2ex

  \hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfill}
\vskip 3ex

\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
\lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\lipsum[1-3]
\end{multicols*}
\end{document}

我尝试通过创建自己的框来测量这一点,该框编译时出现八个错误,并且需要大量使用\hbox和。它还要求我用变体\vbox替换每个实例,这需要胶水(我存在的祸根)。我强烈怀疑这是错误输出的罪魁祸首。然而,我的主要问题是,我目前的方法似乎特别麻烦,这让我相信我的方法不正确,即存在一种更灵活(或者如果你愿意的话可以称为“简单”)的方法。我如何以最方便/灵活的方式创建此框以进行测量并在文档中使用?\hrule\leaders

http://i.imgur.com/o89CdlS.png?1

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\newsavebox\titlebox
\setbox\titlebox=\vbox{%
\hbox{\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
}
\vskip 2ex
\hbox to \linewidth{\textcolor{lightgray}{\leaders\hrule height .5pt\hfil}}
\vskip 6ex
\hbox{\Huge{A title in fontsize Huge}}
\vskip .4ex
\hbox{\large{Doe, J. (John)}}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfil}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfil}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
\vskip2ex
\hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfil}
\vskip 3ex
\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
  \lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
}

\begin{document}
\thispagestyle{plain}

\usebox\titlebox
\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\lipsum[1-3]
\end{multicols*}
\end{document}

答案1

你可以使用tcolorbox软件包(版本3.12 (2014/07/29)或更新版本)进行测量。在我的解决方案中,内核是measurebox由以下代码构建的环境:

\makeatletter
\newtcolorbox{measurebox}[1][]{blank,parbox=false,
  show bounding box,
  finish={\node[draw,fill=white,fill opacity=0.85,inner sep=5mm,
    rounded corners,align=left,node font=\Large\bfseries]
    at (frame.center) { width: \tcb@width,\\ height: \tcb@height};},
  #1}
\makeatother

任何被此环境包围的段落tcolorbox都会被测量,并且其尺寸会写在屏幕或纸上。完整代码如下:

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage[many]{tcolorbox}% version `3.12 (2014/07/29)` or newer

\makeatletter% construct boxing environment
\newtcolorbox{measurebox}[1][]{blank,parbox=false,
  show bounding box,
  finish={\node[draw,fill=white,fill opacity=0.85,inner sep=5mm,
    rounded corners,align=left,node font=\Large\bfseries]
    at (frame.center) { width: \tcb@width,\\ height: \tcb@height};},
  #1}
\makeatother% end construction

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\begin{document}
\thispagestyle{plain}

\begin{measurebox}%----- start measuring -----
\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
\vskip 2ex
\textcolor{lightgray}{\hrule height .5pt}
\vskip 6ex
\Huge{A title in fontsize Huge}
\vskip .4ex
\large{Doe, J. (John)}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
  \vskip2ex

  \hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfill}
\vskip 3ex

\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
\lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
\end{measurebox}%----- end measuring -----
\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\lipsum[1-3]
\end{multicols*}
\end{document}

在此处输入图片描述

环境measurebox采用可选参数,可用于影响框。在此应用程序中,更改框前或框后的间距可能会很有用,例如通过

\begin{measurebox}[before=\par\bigskip,
                   after=\par\vskip 10ex]
...

第二种解决方案:

这是第一部分的变体。现在,框的高度被保存为强制参数以measurebox供进一步使用:

\makeatletter
\newtcolorbox{measurebox}[2][]{blank,parbox=false,
  overlay={\xdef#2{\tcb@height}},
  #1}
\makeatother

它的使用方法如下:

\begin{measurebox}{\myheight}

总的来说,这是:

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}
\usepackage[many]{tcolorbox}

\makeatletter
\newtcolorbox{measurebox}[2][]{blank,parbox=false,
  overlay={\xdef#2{\tcb@height}},
  #1}
\makeatother

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\begin{document}
\thispagestyle{plain}

\begin{measurebox}{\myheight}
\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
\vskip 2ex
\textcolor{lightgray}{\hrule height .5pt}
\vskip 6ex
\Huge{A title in fontsize Huge}
\vskip .4ex
\large{Doe, J. (John)}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
  \vskip2ex

  \hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfill}
\vskip 3ex

\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
\lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
\end{measurebox}
\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\textcolor{red}{The box above has a height of \myheight.}\par
\lipsum[1-3]
\end{multicols*}
\end{document}

在此处输入图片描述

第三种解决方案:

第三个解决方案tcolorbox完全省略。宏\measurebox

\newcommand{\measurebox}[4]{%
  \sbox{#1}{\begin{minipage}{#2}#4%
  \end{minipage}}%
  \edef#3{\the\dimexpr\ht#1+\dp#1\relax}%
}

它有四个参数:#1是有效的盒子名称,#2是宽度,#3是将获取盒子总高度的宏名称,#4是盒子内容。它的使用方式如下:

\newsavebox\titlebox
\measurebox{\titlebox}{\textwidth}{\myheight}{%----- start measuring
...
}%----- end measuring
%
\usebox\titlebox%  somewhere
The box above has a height of \myheight.% somewhere

与上面输出相同的完整代码是:

\documentclass[10pt]{extarticle}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage[a4paper,margin=.9in]{geometry}
\usepackage{xcolor}
\definecolor{palette2}{RGB}{235,249,249}
\usepackage[letterspace=400]{microtype}
\usepackage[english]{babel}
\usepackage{ragged2e}
\usepackage{array}
\usepackage[bitstream-charter]{mathdesign}
\usepackage[T1]{fontenc}
\usepackage{fancyhdr}
\usepackage{etoolbox}

\makeatletter
\newcommand{\measurebox}[4]{%
  \sbox{#1}{\begin{minipage}{#2}#4%
  \end{minipage}}%
  \edef#3{\the\dimexpr\ht#1+\dp#1\relax}%
}
\makeatother

\newcommand{\headrulecolor}[1]{\patchcmd{\headrule}{\hrule}{\color{#1}\hrule}{}{}}
\makeatletter
\fancypagestyle{plain}{
  \fancyhf{}
  \headrulecolor{lightgray}
  \chead{\textit{Some science institution}}
  \renewcommand{\headrulewidth}{.5pt}
}
\setlength\parindent\z@
\newcommand*\docparspacing{
\setlength\parindent{16pt}
\setlength\parskip{0pt}
}
\makeatother

\begin{document}
\thispagestyle{plain}

\newsavebox\titlebox
\measurebox{\titlebox}{\textwidth}{\myheight}{%----- start measuring
\raisebox{3.5ex}{\hbadness=9999
\colorbox{palette2}{\vrule width 0pt height 5.8ex \begin{tabular}{>{\RaggedRight\arraybackslash}p{\dimexpr.3\linewidth-\tabcolsep} >{\centering\arraybackslash}p{\dimexpr.35\linewidth-2\tabcolsep} >{\RaggedLeft\arraybackslash}p{\dimexpr.35\linewidth-4.4\tabcolsep}}
&
&
\end{tabular}}}
\vskip 2ex
\textcolor{lightgray}{\hrule height .5pt}
\vskip 6ex
\Huge{A title in fontsize Huge}
\vskip .4ex
\large{Doe, J. (John)}
\vskip 2ex
\normalsize
\vskip .5ex
\valign{%
    \hsize=\dimexpr.5\linewidth-2\tabcolsep
    \vskip 2ex
    #\vfil\nointerlineskip
    \vskip 1.5ex
    \cr
    \noindent\textls{\itshape\footnotesize KEYWORDS}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex
    \itshape{obituary

    linguist

    typography}\cr
    \noalign{\hfill}
    \noindent\textls{\itshape\footnotesize ABSTRACT}
    \vskip -1.5ex
    \hbox to\hsize{\color{lightgray}\leaders\hrule height .5pt\hfill}
    \vskip 1.5ex

    \small \lipsum[4] \cr
  }\par
  \vskip2ex

  \hbox to\linewidth{\color{lightgray}\leaders\hrule height .5pt\hfill}
\vskip 3ex

\colorbox{palette2}{%
\begin{minipage}{\dimexpr\textwidth-2\fboxsep}
\lipsum[1-2]
\end{minipage}%
}
\vskip 5ex
}%----- end measuring
%
\usebox\titlebox

\docparspacing
\begin{multicols*}{3}
\section{Document structure}
\textcolor{red}{The box above has a height of \myheight.}\par
\lipsum[1-3]
\end{multicols*}
\end{document}

请注意,您必须在之后使用测量\begin{document},但在使用任何一个之前,您可以测量任意数量的盒子。

相关内容