Press Enter in LyX Sweave as You Wish

February 18, 2010
By

(This article was first published on Statistics, R, Graphics and Fun » R Language, and kindly contributed to R-bloggers)

For a long time I’ve been wondering why we are not able to use Enter in the LyX Scrap environment which was set up by Gregor Gorjanc for Sweave. Two weeks ago, I (finally!) could not help asking Gregor about this issue, as I’m using “LyX + Sweave” more and more in my daily work. He explained it here: LyX-Sweave: mandatory use of control+enter in code chunks

After digging into the LyX customization manual for a while, I found a solution which allows us to press the Enter key just as we normally do when typing in a LyX document. The key is to use Environment instead of paragraph as LatexType for the style definition of Scrap. Besides, I used the LatexName as wrapsweave, as a LatexName is required by LyX. The definition for wrapsweave is simple: just two empty lines by par. (If you define it as newenvironment{wrapsweave}{}{}, you will run into troubles sometimes; especially when you use indent for paragraphs.)

As we know, LaTeX environment cannot be centered in LyX (only paragraphs can), so I defined a special environment ScrapCenter when I want to insert graphics via Sweave and make them center-aligned.

To put all efforts together, this is the new literate-scrap.inc (compare it with http://cran.r-project.org/contrib/extra/lyx/literate-scrap.inc):

Format 2

Preamble
usepackage{Sweave}
newenvironment{wrapsweave}{par}{par}
EndPreamble

OutputType              literate

Style Scrap
  LatexType             Environment
  LatexName             wrapsweave
  Margin                static
  TopSep                0.4
  BottomSep             0.4
  ParSep                0.0
  LabelType             static
  Align                 left
  AlignPossible         block, left, right, center
  NewLine               0
  PassThru              1
  FreeSpacing           1
  TextFont
    Color               latex
    Family              typewriter
  EndFont
End

Style ScrapCenter
  LatexType             Environment
  LatexName             center
  Margin                static
  TopSep                0.4
  BottomSep             0.4
  ParSep                0.0
  LabelType             static
  Align                 center
  NewLine               0
  PassThru              1
  FreeSpacing           1
  TextFont
    Color               latex
    Family              typewriter
  EndFont
End

Enjoy pressing a single Enter key in LyX now! :)

P.S. the side effect of this modification is, your R code will be separated by empty lines. But Sweave will remove blank lines by default, so that is not really a big trouble.

P.P.S. Next time I will write my solution of using pgfSweave in LyX — if you have never tried the pgfSweave package in R, I’d strongly recommend you do it right now!! My comment on this package is: amazingly beautiful! See a preview lyx-pgfsweave-demo.

Related Posts

To leave a comment for the author, please follow the link and comment on his blog: Statistics, R, Graphics and Fun » R Language.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , , , ,

Comments are closed.