【问题标题】:Writing text on the border of an A4 PDF in R在 R 中在 A4 PDF 的边框上书写文本
【发布时间】:2013-10-10 16:46:50
【问题描述】:

我需要在 PDF 设备上的页面极端边框上书写。有了这个 sn-p:

pdf('foo.pdf')          #Write next plot to foo.pdf in current dire 
par(mar=c(0, 0, 0, 0))  #Set numbers of lateral blank lines to zero
par(xaxs='i', yaxs='i') #Does not extend axes by 4 percent for pretty labels 
plot.new()              #Create a blank plot, as we just want to write our text  
text(0, .5, "hello", pos=4, offset=0) #Write to the right with no default 0.5 offset
dev.off()               #Close device, that is saving for a PDF device

我得到一个foo.pdfhello 在页面中间的最左边,正如预期的那样:

不幸的是,如果我将纸张输出设置为paper='a4',那就是:

pdf('foo.pdf', paper='a4') #note the A4 setting 
par(mar=c(0, 0, 0, 0))
par(xaxs='i', yaxs='i' )
plot.new()
text(0, .5, "hello", pos=4, offset=0)
dev.off()

hello 不再放在边框上,但是:

【问题讨论】:

    标签: r pdf-generation


    【解决方案1】:

    paper 设置为"special" 以外的其他值时,参数pagecentre 变得相关。如果你设置为FALSE,那么偏移就消失了。

    pdf('foo.pdf', paper='a4', pagecentre=FALSE)
    par(mar=c(0, 0, 0, 0))
    par(xaxs='i', yaxs='i' )
    plot.new()
    text(0, .5, "hello", pos=4, offset=0)
    dev.off()
    

    【讨论】:

    • 您的方法适用于 0-border 但不适用于 1-border。对于 A4 纸,text(0, 1, "hello", pos=4, offset=0) 将位于页面高度的 75% 左右。最糟糕的是,y>1(或x>1)的每一个文本都被认为超出了边界而不被打印出来。
    【解决方案2】:

    好吧,我可以确认它对我也不起作用;恕我直言,它看起来像一个错误。

    解决您的问题的方法是直接在 pdf 调用中设置 A4 纸张尺寸:

    pdf('foo.pdf', width=8.3, height=11.7) #we set A4 dimensions of 8.3 x 11.7 inches
    par(mar=c(0, 0, 0, 0))
    par(xaxs='i', yaxs='i' )
    plot.new()
    text(0, .5, "hello", pos=4, offset=0)
    dev.off()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 2015-04-27
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      • 2012-05-14
      相关资源
      最近更新 更多