I use Lyx (2.1.4) with knitr (1.10.5). I have a lot of trouble having the code chunks respect the width that I tried setting through various ways:

  1. In the chunk options (width=50);
  2. options("width")=50 at the top of my document
  3. opts_chunk$set(width=50) also at the top of my document

The results are always the same:

enter image description here

What am I missing? Any help appreciated.

Edit: I realized that it has to do with the quoted string which won't be broken into several lines. Without long strings, the width setting is respected. (Still, the problem remains unsolved.)

  • Welcome to TeX.SX! If your edit to your question answers your question, you should consider providing an actual answer, rather than just editing your question.
    – Adam Liter
    Aug 9, 2015 at 1:44
  • It doesn't answer the question, just points out more precisely where the problem is... I added a small sentence at the end to make that clear. Aug 9, 2015 at 2:08
  • If you don't consider it an answer, you might wish to provide a minimal working example (MWE) for folks to play with. (See LyX user questions on TeX stackexchange.) However, I'd also point out that this is ultimately a problem for R, not TeX/LyX, at least the way that you want to do it. See How to wrap text in R source with tidy and knitr on Stack Overflow. There are some hacky solutions there. If you don't want a hacky solution, use listings or minted.
    – Adam Liter
    Aug 9, 2015 at 2:13
  • You can use listings with knitr, but I'm not super familiar with how this works.
    – Adam Liter
    Aug 9, 2015 at 2:15
  • There's actually a good answer here that shows how to use the breaklines option of listings with knitr. Does this help?
    – Adam Liter
    Aug 9, 2015 at 2:31

1 Answer 1


Ok so thanks to Adam's help, I was able to see there actually is a workaround for this. However, it involves a fair bit of tweaking in order to make the chunks appear the same way as my other chunks do. So for now, my solution was to set tidy=FALSE and eval=FALSE in the chunk options, and put this in the chunk:

boxplot(fumeurs$imc, horizontal=TRUE, main="Distribution de 
+    l'indice de masse corporelle", xlab="IMC (Kg/m^2)")

That way, the chunk appears exactly as would another one (that is, without spilling over the margins). Then, in another chunk just after (or just before) this one, I create another one having echo=FALSE as an option, and put into it the R code as it is intended (with the long string unbroken). Kinda dirty, but it seems that it is the best fix that doesn't require a full day of tweaking around.

See the links in Adam's comments to my question if you are willing to go through the listings + hook route, and best of luck to you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.