问题
PDF
我想控制我制作的文档的页面高度LaTeX
。
LaTeX
从一个文档和两个参数开始:
- A给定页面大小 (
heightPage
和widthPage
) - A给定长度称为
initialVerticalInset
,
我想制作一个PDF
页面长度符合以下模式的
- 页面的正常高度是
heightPage
- 但,第一页的高度为
(heightPage - initialVerticalInset)
- 和,最后一页的高度等于“其内容的高度”,即在其内容结束后“最后一页被剪切”
如果可能的话,我还想:
- 如果将任何页面放在另一页面旁边,则基线应该对齐
- 可以将边距设置为最小值(例如
0pt
)
示例 1
示例 2
需要测试的最少文档
在一个以前类似的问题,我注意到一个困难可能是与以下内容的兼容性:
itemize
环境mdframed
环境- 还必须处理短文件的情况。
因此,我准备了三个 MWE 进行测试。
简短文件
\documentclass{article}
\usepackage[paperwidth=8cm, paperheight=10cm, hmargin=0mm,
top=0mm, bottom=0mm] {geometry}
\setlength{\initialVerticalInset}{4.5cm}
\begin{document}
This is a test. This is a longer sentence with some more words, isn't it?
\end{document}
itemize
带环境的文档
\documentclass{article}
\usepackage[paperwidth=8cm, paperheight=10cm, hmargin=0mm,
top=0mm, bottom=0mm] {geometry}
\pagestyle{empty}
\setlength{\initialVerticalInset}{4.5cm}
\begin{document}
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test:
\begin{itemize}
\item Hello
\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.
\item Good Bye
\item Hello
\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.
\end{itemize}
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
\end{document}
mdframed
带环境的文档
\documentclass{article}
\usepackage[
paperwidth=15cm,
paperheight=15cm,
hmargin=1cm,
vmargin=0cm,
]{geometry}
\usepackage[framemethod=tikz, needspace=1.5cm]{mdframed}
\newmdenv[%
innerleftmargin = 2mm,
innerrightmargin = 2mm,
innertopmargin = 2mm,
innerbottommargin = 2mm,
leftmargin = 0mm,
rightmargin = 0mm,
splitbottomskip = 2mm,
splittopskip = 4mm,
middlelinewidth = 0mm,
linecolor = red,
backgroundcolor = red,
roundcorner = 0pt,
skipbelow = 0mm,
skipabove = 0mm,
]{mybox}
\pagestyle{empty}
\setlength{\initialVerticalInset}{5cm}
\begin{document}
\begin{mybox}
\lipsum[1]
\end{mybox}
\lipsum[1-7]
\end{document}
答案1
我认为这个方法有效。感谢这个想法\pdfpageheight=\pagetotal
wipet 的回答如何精确控制每个页面的大小?
\documentclass{article}
\newlength{\initialVerticalInset}
\setlength{\initialVerticalInset}{4.5cm}
\usepackage[paperwidth=10cm,
paperheight=-\dimexpr\initialVerticalInset- 10cm\relax,
margin=0mm]{geometry}
\makeatletter
\newcommand{\setnewvsize}[1]{%
\global\paperheight\dimexpr#1+\initialVerticalInset\relax%
\global\pdfpageheight\paperheight%
\global\textheight\paperheight%
\global\@colht\textheight \global\@colroom\textheight \global\vsize\textheight%
\global\initialVerticalInset=0pt}
\let\mt@outputpage\@outputpage
\def\@outputpage{\mt@outputpage\setnewvsize\paperheight}
\makeatother
\AtEndDocument{\initialVerticalInset=0pt\vskip-\lastskip\nobreak\par\vskip1pt\par\setnewvsize\pagetotal}
\usepackage{lipsum}
\begin{document}
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test:
\begin{itemize}
\item Hello
\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.
\item Good Bye
\item Hello
\item This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test.
\end{itemize}
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
This is a test. This is a longer sentence with some more words, isn't it? This is a test. This is a longer sentence with some more words, isn't it?
\end{document}
逐项列举
框架
仅限示例 2 页
例如只有一页
答案2
这个答案有问题
漂浮物体
我尝试查看此答案是否适用于figure
环境。如果它们出现在最后一页,则它无法按预期工作(最后一页有\normalHeigt
而不是“裁剪高度”)
PS:由于我对它一无所知TeX
,也不是它的超级用户LaTeX
,所以可能存在错误或语法错误。如果您认为以下代码可以改进,请评论!
回答
这个答案是在touhami的答案的基础上进行阐述的。
\documentclass{article}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newlength{\initialHeight}
\setlength{\initialHeight}{5.5cm}
\newlength{\normalHeight}
\setlength{\normalHeight}{10cm}
\newlength{\normalWidth}
\setlength{\normalWidth}{10cm}
\newlength{\horizontalMargin}
\setlength{\horizontalMargin}{0mm}
\newlength{\topMargin}
\setlength{\topMargin}{0mm}
\newlength{\bottomMargin}
\setlength{\bottomMargin}{0mm}
% This is a crucial line
% see http://tex.stackexchange.com/a/268943/8323
\setlength{\maxdepth}{0pt}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Size
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage[paperwidth=\normalWidth,
paperheight=\initialHeight,
left=\horizontalMargin,
right=\horizontalMargin,
top=\topMargin,
bottom=\bottomMargin]{geometry}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Core
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
% Set the vertical size of the page
%
\newcommand{\setnewvsize}[1]{%
\global\paperheight\dimexpr#1\relax%
\global\pdfpageheight\paperheight%
\global\textheight\dimexpr\paperheight - \bottomMargin - \topMargin\relax%
\global\@colht\textheight%
\global\@colroom\textheight%
\global\vsize\textheight%
}
% Change the vsize after the first page
% (after the first output)
%
\newcommand\firstPassageInOutput{1}
\let\mt@outputpage\@outputpage
\def\@outputpage{\mt@outputpage\ifnum\firstPassageInOutput=1\setnewvsize\normalHeight\renewcommand\firstPassageInOutput{0}\fi}
% Change the vsize after the last page
%
\AtEndDocument{%
% The following line is needed is the case when the last page is only one line
\vskip-\lastskip\nobreak\par\vskip0pt\par
% Core
\global\paperheight\dimexpr\pagetotal + \bottomMargin + \topMargin \relax%
\global\pdfpageheight\paperheight%
\global\textheight\dimexpr\pagetotal
}
\makeatother
\usepackage{lipsum}
\begin{document}
lipsum{}
\end{document}