How to get sweave to centers without centering code

One way to get sweave to center figure is to include something like the following

\begin{figure} \begin{center} [sweave chunk] \end{center} \end{figure} 

However, if you want to display the code to create the visualization, the code exits the center. Does anyone know how many way to make the code appear left-aligned while still checking that the shape is centered? This is problematic if you set the size of the shape to say 3x3:

 <<myplot, fig=TRUE, width=3, height=3>>= plot(rnorm(20), rnorm(20)) @ 

I need to do this more than 100 times for the project I'm working on, and would prefer not to create separate fragments for the graph and code.

+6
source share
1 answer

Maybe not quite what you need, but I like to save the code that I use to make the image outside the drawing area, since I want the code in the text:

 Blablabla if we run: R CODE We get the figure in Figure 1 

You can do this by using include=false in the Sweave arguments and using the fact that if you take a picture in Sweave, it calls pdf DOCUMENTNAME-PICTURENAME.pdf . For example, in the document foo.Rnw :

 Blablabla if we run: <<myplot,fig=true,include=false>>= plot(1) @ We get the plot in Figure \ref{myplot} \begin{figure} \begin{center} \includegraphics{foo-myplot} \end{center} \label{myplot} \caption{This is my plot!} \end{figure} 

This should cause your code to remain justified in the current text and figure where LaTeX places it (if you want right there).


Edit:

Running your example causes me to leave justified code in the same field as plain text:

 \documentclass{article} \usepackage{Sweave} \begin{document} \begin{figure} \centering <<myplot, fig=TRUE, width=3, height=3>>= plot(rnorm(20), rnorm(20)) @ \end{figure} \end{document} 
+6
source

Source: https://habr.com/ru/post/890827/


All Articles