使用knitr编译为pdf时如何控制输出宽度? [英] How to control output width when use knitr to compile to pdf?

查看:118
本文介绍了使用knitr编译为pdf时如何控制输出宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用knitr将.Rnw文档编译为pdf,大块中输出文本的宽度不能通过out.width选项重置.

using knitr to compile to a .Rnw document to a pdf, the width of output text in a chunk can not be resetted by out.width option.

如何将输出文本保留在具有背景色的区域中?

how to keep the output text in area with the background color?

推荐答案

PDF中的文本输出宽度始终是一个痛苦的问题.要确保所有内容都适合页面,这并不容易.两种可能的通用解决方案(如果您必须使用LaTeX/PDF):

The text output width in PDF is always a painful problem. It is not easy to make sure everything fits on a page. Two possible and general solutions (if you have to use LaTeX/PDF):

  1. 在LaTeX中缩小白色边距,例如

  1. make the white margin smaller in LaTeX, e.g.

\usepackage{geometry}
\geometry{verbose,tmargin=3cm,bmargin=3cm,lmargin=3cm,rmargin=3cm}

  • 使文本输出在R中变窄,例如

  • make the text output narrower in R, e.g.

    options(width = 60)
    

  • 请注意,不能保证这些解决方案都能正常工作,因为您无法确定文本输出的宽度,并且某些输出函数不遵守width选项.例如,几乎不可能完成这项工作:

    Note these solutions are not guaranteed to work, because you can never be sure how wide the text output can be, and some output functions do not respect the width option. For example, it is almost impossible to make this work:

    cat('This is a long long long long long long long long long long long long long long long long long long long long long long long long string. Haha.\n')
    

    有些LaTeX软件包(例如listings)可以包装逐字输出,但也会导致输出难看.例如,如果R的自然输出是这个(假设两行太宽):

    There are some LaTeX packages such as listings that can wrap the verbatim output, but that can also lead to ugly output. For example, if the natural output from R is this (suppose the two lines are too wide):

    text output wrapped by R
    and then wrapped by LaTeX
    

    LaTeX包装可以将其变为:

    LaTeX wrapping can turn it to this:

    text output wrapped
    by R
    and then wrapped by
    LaTeX
    

    对于显示列联表的特定应用,我的建议是转置表(t(table(...)))并将其打印.

    For your specific application of showing a contingency table, my suggestion is to transpose the table (t(table(...))) and print it.

    这篇关于使用knitr编译为pdf时如何控制输出宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆