By default, knitr uses a LaTeX package named framed for typesetting; the most obvious feature is the light gray shading. In this page, we introduce some tricks and known problems.
As listed in FAQ’s, you may see the output overflow the shading box, and you can set options('width') to a smaller value in this case.
Overflow of elements
Besides text overflow, figures may also exceed the margin of the shading. If a figure is too wide, LaTeX may complain there is something wrong with the kframe environment, which is what knitr uses to wrap up the chunk output. A known case is about PNG graphics in #154. To make sure your figures do not exceed the page margin, knitr uses the following command in the LaTeX preamble:
%% maxwidth is the original width if it's less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
The chunk option out.width is set to '\\maxwidth' by default if the output format is LaTeX.
Padding of the shading box
This LaTeX command sets the padding to be 5mm if you feel the default layout is too parsimonious (no padding at all).
\setlength\fboxsep{5mm}
If you do not like this default style with the framed package, you are free to switch to other styles like listings or even define your own output hooks.
Incompatible environments with framed
It does not work well with the figure* environment in two-column documents; see knitr-twocolumn.pdf for one approach to deal with this situation.
If you use the Tufte handout/book classes, the fullwidth environment does not work well with the framed package either; see discussion in #222 for possible solutions.
It does not work with the lineno package; see Michael’s post in the mailing list.
Donate
As a freelancer (currently working as a contractor) and a dad of three kids, I truly appreciate your donation to support my writing and open-source software development! Your contribution helps me cope with financial uncertainty better, so I can spend more time on producing high-quality content and software. You can make a donation through methods below.
-
Venmo:
@yihui_xie, or Zelle:xie@yihui.name -
Paypal
-
If you have a Paypal account, you can follow the link https://paypal.me/YihuiXie or find me on Paypal via my email
xie@yihui.name. Please choose the payment type as “Family and Friends” (instead of “Goods and Services”) to avoid extra fees. -
If you don’t have Paypal, you may donate through this link via your debit or credit card. Paypal will charge a fee on my side.
-
-
Other ways:
WeChat Pay (微信支付:谢益辉) Alipay (支付宝:谢益辉) 

When sending money, please be sure to add a note “gift” or “donation” if possible, so it won’t be treated as my taxable income but a genuine gift. Needless to say, donation is completely voluntary and I appreciate any amount you can give.
Please feel free to email me if you prefer a different way to give. Thank you very much!
I’ll give back a significant portion of the donations to the open-source community and charities. For the record, I received about $30,000 in total (before tax) in 2024-25, and gave back about $15,000 (after tax).