無法僅為程式碼區塊設定單行距

無法僅為程式碼區塊設定單行距

我只是將問題放在範例 .Rnw 程式碼區塊中。當將其編譯為 .pdf 檔案時,最容易發現問題。我正在使用 pdfLaTeX 和 knit 從 RStudio 中編譯文件。請注意,程式碼區塊也在 R 中運行非常重要,因為它們可能會執行諸如告訴 R 繪製繪圖之類的操作,這些繪圖將成為文件的一部分。

\documentclass{article}
\usepackage{setspace}
\doublespacing
\begin{document}
\section*{Question Part 1.}
I am writing this document inside RStudio, using Sweave. I want everything 
in the document to be doublespaced with the exception of R code chunks, 
which I want to be singlespaced for the sake of tidiness and saving space. 
It is important that the code chunks also run in R, since they may be doing 
things like telling R to draw plots which will be part of the document.
\section*{Question Part 2.}
The problem that I seem to be having is that when I put in a command to 
begin singlespacing and a command to end single spacing around an R code 
chunk, text that is not inside the begin and end singlespacing command, for 
some reason, ends up being singlespaced as well, even though as far as I can 
tell it should be doublespaced, as can be seen with the text in this very 
paragraph (if you've compiled it into a .pdf).
\begin{singlespacing}
<<echo=TRUE>>=
k=3
mse=0.347
d=0.85
alpha=0.05
n=30
pwr.df=data.frame(row.names = 1:n)
pwr.df$n=1:n
@
\end{singlespacing}
Strangely, the problem doesn't occur for text which follows the "end" of the 
singlespacing commmand, but text which comes before the 'begin' 
singlespacing command is affected, as can be seen here.
\end{document}

答案1

我不確定問題的原因是什麼,但如果你將程式碼放入 a 中,minipage它就會以你想要的方式運作:

\documentclass{article}
\usepackage{setspace}
\begin{document}
\doublespacing
\section*{Question Part 1.}
I am writing this document inside RStudio, using Sweave. I want everything 
in the document to be doublespaced with the exception of R code chunks, 
which I want to be singlespaced for the sake of tidiness and saving space. 
It is important that the code chunks also run in R, since they may be doing 
things like telling R to draw plots which will be part of the document.
\section*{Question Part 2.}
The problem that I seem to be having is that when I put in a command to 
begin singlespacing and a command to end single spacing around an R code 
chunk, text that is not inside the begin and end singlespacing command, for 
some reason, ends up being singlespaced as well, even though as far as I can 
tell it should be doublespaced, as can be seen with the text in this very 
paragraph (if you've compiled it into a .pdf).

\begin{minipage}{\linewidth}
\singlespacing
<<echo=TRUE>>=
k=3
mse=0.347
d=0.85
alpha=0.05
n=30
pwr.df=data.frame(row.names = 1:n)
pwr.df$n=1:n
@
\end{minipage}
Strangely, the problem doesn't occur for text which follows the "end" of the 
singlespacing commmand, but text which comes before the 'begin' 
singlespacing command is affected, as can be seen here.
\end{document}

程式碼的輸出

相關內容