測量頁面的一部分

測量頁面的一部分

我正在嘗試測量頁面一部分的高度。

完整的標題頁以及我試圖測量的頁面部分以紅色勾勒,編譯頁面的程式碼如下所示。是的,我必須一次測量所有這些。:-(

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},但在使用任何一個之前,您可以測量任意數量的盒子。

相關內容