问题描述
我正在使用knitr编写教程,我想展示学生可能会遇到的一些警告和错误.虽然我可以使用tidy=TRUE
选项在框内很好地显示代码块,但我不理解如何处理警告和错误的显示.例如,如果我有以下代码:
I'm writing a tutorial using knitr and I want to show some of the warnings and errors that students might encounter. While I'm capable of nicely displaying the code chunks within the box using the tidy=TRUE
option, I don't understand how to handle the display of warnings and errors. For example, if I have the following code:
\documentclass{article}
\begin{document}
<<setupOp, include=FALSE>>=
opts_chunk$set(tidy=TRUE,
tidy.opts=list(blank=FALSE, width.cutoff=20))
@
<<ErrorTest>>=
warning(paste("A suuuuuuuuuuuuuuuuuper", "loooooooooooong waaaaaaaaaaaaarning"))
@
\end{document}
警告代码行很好地显示在框中,但是警告本身超出了该框.我感觉警告消息是一个很长的字符串,但这与事实有关,但是我不知道如何告诉knitr将警告信息保留在盒子内.我看过编织程序块选项上的文档,以及此 formatR信息,但我找不到解决方法.
The warning code line is nicely displayed within the box, but the warning itself stretch beyond the box. I have a feeling that it has to do with the fact that the warning message is one very long string, but I don't know how to tell knitr to keep the warning inside the box. I've looked at the documentation on knitr chunk options and this formatR info, but I can't find the solution.
谢谢!
推荐答案
LaTeX试图在此处生成左右对齐的文本块.这意味着自动换行和连字符以得到一个不错的笔直的右边缘.您的警告内容很长,LaTeX不会为打字机文本加上连字号,因此它会填满为其分配的框,并在TeX日志文件中显示过度警告.
LaTeX is trying to generate a left and right justified block of text here. That means word-wrapping and hyphenating to get a nice straight right edge. Your warning has long words in it, LaTeX doesn't hyphenate typewriter text, so it overfills the box allocated for it and prints an overfull warning to the TeX log file.
即使可以对文本进行连字,也可能很难找到一个对奇数词进行连字的好地方.例如,您永远不要跨行将按钮式"中断为但色调". TeX为此有一个复杂的算法.
Even if it could hyphenate the text, it might struggle finding a good place to hyphenate an odd word. For example, you should never break "buttoned" across lines as "but-toned". TeX has a complicated algorithm for this.
一种解决方案可能是为您的R块设置\ raggedright:
A solution may be to set \raggedright for your R blocks:
{
\raggedright
<<setupOp, include=FALSE>>=
opts_chunk$set(tidy=TRUE,
tidy.opts=list(blank=FALSE, width.cutoff=20))
@
<<ErrorTest>>=
warning(paste("A suuuuuuuuuuuuuuuuuper", "loooooooooooong waaaaaaaaaaaaarning"))
@
}
像这样,只要有一个单词出现在框外,TeX应该开始换行.将花括号括起来,使普通文本不受影响.我不知道这还会在代码块中影响什么,所以请警告购买者.
Like this, TeX should start a new line whenever a word would go outside the box. Enclose in a curly-bracket pair so normal text is unaffected. I don't know what else this might affect within the code block, so caveat emptor.
这篇关于Knitr如何将警告消息保留在包装箱内?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!