有没有办法用 LaTeX 将两页合并为一页?

有没有办法用 LaTeX 将两页合并为一页?

我希望使用 LaTeX 将我的文档编译为两页合一,就像您选择将两页合一打印时看到的那样。由于我的页面是“纵向”的,因此当两页合一时,我需要页面以横向显示,如果我还可以在每页周围添加边距,那就太好了。

是否有人知道我可以使用哪些包来获得我想要的效果?

答案1

你可以使用pdf页面或者pdfjam及其 pdfnup 工具。

答案2

我突然想到,无需后期处理即可将两页合并为一页。那么,直接制作两列文档如何?

\documentclass[twocolumn]{article}
\usepackage[landscape,margin=2cm]{geometry}

答案3

安德鲁有一个非常明智的想法不是将文件缩小到 71%,因为这会使文本相当小,而且通常你不希望出现过多的白色边距。我的做法是:首先放大根据需要使用我下面提供的 shell 脚本(按照 Andrew 的建议)编辑文件,然后使用您喜欢的任何工具将 2 或 4(或更多)页放在一个页面上。我必须指出,我在这里假设您使用的是 postscript 文件。是的,当您使用时,您确实会丢失所有超链接和类似的东西,pdftops但我认为这没什么问题,因为我只会将其用于打印。

我的脚本相当长,因为它试图变得聪明。你实际上不需要使用它的说明;只需输入脚本的名称(我这样称呼它,gr因为它使事情变得更大),不带任何参数,它就会打印一些使用信息。我在文件中加了一些注释,以解释更多。有一件事我应该解释一下:我经常使用一个很好的功能,即所谓的“groptions”。假设您有一个 TeX 文件“document.tex”,输出为“document.ps”。如果在 document.tex 的前 10 行中有一行

%groptions: 15 thisfile -u4

然后你可以通过以下方式调用脚本

gr document

这将产生与

gr 15 document -u4

即:放大 15%,向上移动 4 毫米。因此您不必总是记住特定文件的参数。

# In the following comments I assume that this script is called "gr".
# You can also call it "mag" or whatever if you wish;
# the builtin command line help will understand this.

gv=false
[ "$1" = --gv ] && gv=true && shift
# This opens the output file with gv if the first parameter is --gv .
# I have aliased "gr" to "gr --gv", so that the --gv option
# is always selected if I type "gr" on a command line,
# and it is not selected if "gr" is called in a shell script.

# If you have a file <file>.ps, you can use
#      gr 20 <file>
# or   gr 20 <file>.
# or   gr 20 <file>.ps
# to magnify it by 20%.  The default output file is "gr.ps".
# The rationale behind this:
# If you have <file>.tex and <file>.ps in your directory,
# and you use the tab key for expanding the file name,
# then you'll only get "<file>.", and you don't have to type "ps".
#
# If the input file comes from standard input, use "gr 20 -".
if [ $# -eq 1 ]
then infile="${1%ps}"
     infile="${infile%.}"
     [ -f "$infile.tex" ] &&
     grop=`head "$infile.tex" | grep groptions | dos2unix` &&
     set -- `echo -n "$grop" | sed "s|.*groptions: ||;s|thisfile|$infile|"`
fi

compute () {
arg=`echo "$@" | sed 's|--|+|g'`
echo | awk "{print $arg}" | tr , .
}

eval "set -- `getopt -sbash r:l:u:d:o:i:x:y:2 "$@"`"
right=0
up=0
outward=0
xmin=0
xmax=595
ymin=0
ymax=842
fromsecond=false
while [ $# -gt 0 ]
do case "$1" in
    -r) right=`compute $right+$2`; shift;;
    -l) right=`compute $right-$2`; shift;;
    -u) up=`compute $up+$2`; shift;;
    -d) up=`compute $up-$2`; shift;;
    -o) outward=`compute $outward+$2`; shift;;
    -i) outward=`compute $outward-$2`; shift;;
    -x) xmin=${2%-*}; xmax=${2#*-}; shift;;
    -y) ymin=${2%-*}; ymax=${2#*-}; shift;;
    -2) fromsecond=true;;
    --) shift; break;;
   esac
   shift
done

[ -z "`echo "$1" | sed 's|[0-9.]||g'`" ] && proz=$1 && shift || proz=0 

[ $# -eq 0 ] && echo "    ${0##*/} -- magnifies the pages of postscript files
Usage: ${0##*/} <how many %> <which file>
       3rd parameter (optional): output file (without suffix .ps)
       options: -2 -- magnify only from second page on
                -l<number> -- shift left by <number>mm
                -r<number> -- shift right by <number>mm
                -u<number> -- shift up by <number>mm
                -d<number> -- shift down by <number>mm
                -o<number> -- shift outward by <number>mm
                -i<number> -- shift inward by <number>mm
                -x<min>-<max> -- cropping the x-scope <min>-<max>pt (default 0-595)
                -y<min>-<max> -- cropping the y-scope <min>-<max>pt (default 0-842)" && exit 0

outfile="${2:-gr}.ps"
if [ "$1" = - ]
then infile=/tmp/tmp$$infile.ps
     cat > $infile
     what='"$infile"'
# you can also use this script on .ps.gz files.
else infile="${1%.gz}"
     infile="${infile%ps}"
     infile="${infile%.}"
     # This is a good place where one could add conditional compilation
     # of the input file. For me this is:
     # condlatex "$infile" || exit 1
     # conddvips "$infile"
     infile="$infile.ps"
     [ -f "$infile" ] && what='"$infile"' ||
       if [ -f "$infile.gz" ]
         then unzip="gunzip -c '$infile.gz' |" && what=
         else echo "»$infile« not found" && exit 0
       fi
fi
right=`compute $right-$proz*1.05`
up=`compute $up-$proz*1.48`
proz=`compute 1+$proz/100`

pages () {
page=$(eval "$unzip head -n15 $what" | grep '%%Pages:* ' | cut -d' ' -f2 | dos2unix)
[ "$page" = "(atend)" ] && 
eval "$unzip tail $what" | grep '%%Pages:* ' | cut -d' ' -f2 | dos2unix ||
echo $page
}

if [ "$outward" = 0 ] && [ "$fromsecond" = false ]
then str="1:0@$proz(${right}mm,${up}mm)"
else pages=`pages`
     rpo=`compute $right+$outward`
     rmo=`compute $right-$outward`
     [ "$fromsecond" = true ] && str="$pages:0@1" ||
                           str="$pages:0@$proz(${rpo}mm,${up}mm)"
     p=1
     while [ $p -lt $pages ]
     do str="$str,$((p++))@$proz(${rmo}mm,${up}mm)"
    [ $p -lt $pages ] &&
    str="$str,$((p++))@$proz(${rpo}mm,${up}mm)"
     done
fi
eval "$unzip pstops -pa4 '$str' $what" | sed '
/^%%Page:* /N
/^userdict\/PStoPSsaved save put$/N
/^%%Page:* .*\nuserdict\/PStoPSsaved save put/,/^ closepath}put initclip$/{
/^userdict\/PStoPSclip{0 0 moveto$/,/^ closepath}put initclip$/d
/^PStoPSmatrix setmatrix$/a    userdict/PStoPSclip{'"$xmin $ymin"' moveto    '"$xmax $ymin lineto $xmax $ymax lineto $xmin $ymax"' lineto    closepath}put initclip
}
' > "/tmp/tmp$$$outfile"
mv "/tmp/tmp$$$outfile" "$outfile"
if $gv; then
  ps w | grep "[^]] gv $outfile" >/dev/null || gv "$outfile" &
fi
[ "$outfile" = gr.ps ] && echo "output in gr.ps"
[ $proz = 1 ] && echo '         No magnification !!!'
[ "$infile" = /tmp/tmp$$infile.ps ] && rm $infile

答案4

这是 Stefan 答案的扩展,建议使用页面。我刚刚尝试使用2 on 1布局。我使用负长度参数border shrink使页面更大,以更好地适应页面,但发现页面没有正确居中(在最佳尺寸下,页码会从页面底部丢失)。所以我扩展了布局2 on 1以采用水平和/或垂直移动参数来移动页面的中心。我将其发布在这里,以防对其他人有用。由于一个命令,它需要放在单独的文件中.sty或夹在 中\makeatletter ... \makeatother

\define@key{pgfpagesuselayoutoption}{horizontal shift}%
{\def\pgfpageoptionhshift{#1}}
\define@key{pgfpagesuselayoutoption}{vertical shift}%
{\def\pgfpageoptionvshift{#1}}

\pgfpagesdeclarelayout{2 on 1 shifted}
{
  \edef\pgfpageoptionheight{\the\paperwidth} % landscaped by default
  \edef\pgfpageoptionwidth{\the\paperheight}
  \def\pgfpageoptionborder{0pt}
  \def\pgfpageoptionfirstshipout{1}
  \def\pgfpageoptionhshift{0pt}
  \def\pgfpageoptionvshift{0pt}
}
{
  \pgfpagesphysicalpageoptions
  {%
    logical pages=2,%
    physical height=\pgfpageoptionheight,%
    physical width=\pgfpageoptionwidth,%
    current logical shipout=\pgfpageoptionfirstshipout%
  }
  \ifdim\paperheight>\paperwidth\relax
    % put side-by-side
    \pgfpageslogicalpageoptions{1}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=\pgfphysicalheight,%
      center=\pgfpoint{.25\pgfphysicalwidth+\pgfpageoptionhshift}{.5\pgfphysicalheight+\pgfpageoptionvshift}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=.5\pgfphysicalwidth,%
      resized height=\pgfphysicalheight,%
      center=\pgfpoint{.75\pgfphysicalwidth+\pgfpageoptionhshift}{.5\pgfphysicalheight+\pgfpageoptionvshift}%
    }%
  \else
    % stack on top of one another
    \pgfpageslogicalpageoptions{1}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpoint{.5\pgfphysicalwidth+\pgfpageoptionhshift}{.75\pgfphysicalheight+\pgfpageoptionvshift}%
    }%
    \pgfpageslogicalpageoptions{2}
    {%
      border shrink=\pgfpageoptionborder,%
      resized width=\pgfphysicalwidth,%
      resized height=.5\pgfphysicalheight,%
      center=\pgfpoint{.5\pgfphysicalwidth+\pgfpageoptionhshift}{.25\pgfphysicalheight+\pgfpageoptionvshift}%
    }%
  \fi    
}

使用方法:

\usepackage{pgfpages}

... now put or load the above code ...

\pgfpagesuselayout{2 on 1 shifted}[border shrink=-3cm,vertical shift=.5cm, horizontal shift=2cm]
\nofiles

目前,两个页面的移动量相同,方向也相同。扩展它以独立移动这两个页面会很容易。

相关内容