LyX中插入不执行的R代码块至Algorithm Float程序清单的问题
解决LyX中Algorithm Float内插入R代码清单的问题
问题背景
我需要在LyX中完成以下操作:
- 通过
Insert > Float > Algorithm插入算法浮动环境,再在其中插入Program Listing,将一段R代码作为纯代码清单展示 - 待插入的代码块:
<<A-simple-plot>>= x<-rnorm(10) y<-rnorm(10) plot(x,y) @
- 编译后得到的非预期输出:
\begin{knitrout} \definecolor{shadecolor}{rgb}{0.969,0.969,0.969}\color{fgcolor}\begin{kframe} \begin{alltt} \hlstd{x}\hlkwb{<-}\hlkwd{rnorm}\hlstd{(}\hlnum{10}\hlstd{)} \hlstd{y}\hlkwb{<-}\hlkwd{rnorm}\hlstd{(}\hlnum{10}\hlstd{)} \hlkwd{plot}\hlstd{(x, y)} \end{alltt} \end{kframe} \includegraphics[width=\maxwidth]{figure/unnamed-chunk-3-1} \end{knitrout}
上述输出包含了knitr执行后的格式代码和生成的图片,并非我想要的纯代码清单,目前还未尝试Verbatim包,求解决方法。
解决方案
问题根源是文档启用了knitr模块,直接将knitr代码块放入Program Listing会被knitr自动编译执行,而非当作纯文本展示。以下三种方法可解决:
方法一:修改knitr代码块选项(最简单,保留语法高亮)
给knitr代码块添加eval=FALSE选项,让knitr只渲染代码、不执行代码生成输出:
<<A-simple-plot, echo=TRUE, eval=FALSE>>= x<-rnorm(10) y<-rnorm(10) plot(x,y) @
将修改后的代码块插入Algorithm Float内的Program Listing中,编译后只会输出带语法高亮的纯代码,不会生成图片。
方法二:使用Verbatim环境(纯文本展示)
- 加载verbatim包:LyX中依次点击
Document > Settings > LaTeX Preamble,添加代码:
\usepackage{verbatim}
- 在Algorithm Float中插入ERT(
Insert > TeX Code),用verbatim环境包裹代码:
\begin{verbatim} <<A-simple-plot>>= x<-rnorm(10) y<-rnorm(10) plot(x,y) @ \end{verbatim}
编译后会直接显示纯文本代码,无语法高亮,但不会被knitr解析执行。
方法三:使用listings包(自定义语法高亮,推荐)
- 在LaTeX导言区添加listings包及样式设置:
\usepackage{listings} \usepackage{xcolor} \lstset{ language=R, basicstyle=\ttfamily, keywordstyle=\color{blue}, commentstyle=\color{green}, stringstyle=\color{red}, breaklines=true, frame=single }
- 在Algorithm Float中插入ERT,用lstlisting环境包裹代码:
\begin{lstlisting} <<A-simple-plot>>= x<-rnorm(10) y<-rnorm(10) plot(x,y) @ \end{lstlisting}
此方法可生成带自定义语法高亮的纯代码清单,且不会被knitr执行。
最小工作示例(MWE)
#LyX 2.3 created this file. For more info see http://www.lyx.org/ \lyxformat 544 \begin_document \begin_header \save_transient_properties true \origin unavailable \textclass article \use_default_options true \begin_modules knitr \end_modules \maintain_unincluded_children false \language english \language_package default \inputencoding auto \fontencoding global \font_roman "default" "default" \font_sans "default" "default" \font_typewriter "default" "default" \font_math "auto" "auto" \font_default_family default \use_non_tex_fonts false \font_sc false \font_osf false \font_sf_scale 100 100 \font_tt_scale 100 100 \use_microtype false \use_dash_ligatures true \graphics default \default_output_format default \output_sync 0 \bibtex_command default \index_command default \paperfontsize default \spacing onehalf \use_hyperref false \papersize default \use_geometry false \use_package amsmath 1 \use_package amssymb 1 \use_package cancel 1 \use_package esint 1 \use_package mathdots 1 \use_package mathtools 1 \use_package mhchem 1 \use_package stackrel 1 \use_package stmaryrd 1 \use_package undertilde 1 \cite_engine basic \cite_engine_type default \biblio_style plain \use_bibtopic false \use_indices false \paperorientation portrait \suppress_date false \justification true \use_refstyle 1 \use_minted 0 \index Index \shortcut idx \color #008000 \end_index \secnumdepth 3 \tocdepth 3 \paragraph_separation indent \paragraph_indentation default \is_math_indent 0 \math_numbering_side default \quotes_style english \dynamic_quotes 0 \papercolumns 1 \papersides 1 \paperpagestyle default \tracking_changes false \output_changes false \html_math_output 0 \html_css_as_file 0 \html_be_strict false \end_header \begin_body \begin_layout Title Minimal Working Example with Knitr Program Listing using Algorithm Floats in LyX \end_layout \begin_layout Author Diego A. Acosta M., PhD \end_layout \begin_layout Date 2024-01-10 \end_layout \begin_layout Abstract This is a minimal working example of a LyX article that aims at including \begin_inset ERT status open \begin_layout Plain Layout \backslash sff{R} \end_layout \end_inset and executing \begin_inset ERT status open \begin_layout Plain Layout \backslash sff{R} \end_layout \end_inset code chunks and showing a program listing of an \begin_inset ERT status open \begin_layout Plain Layout \backslash sff{R} \end_layout \end_inset code chunk without executing it. For this to happen the module Rnw (knitr) needs to be added to this document (Document ▷ Settings ▷ Modules ▷ Rnw (knitr)) as well as have knitr installed in \begin_inset ERT status open \begin_layout Plain Layout \backslash sff{R} \end_layout \end_inset . The path \begin_inset ERT status open \begin_layout Plain Layout \backslash sff{R} \end_layout \end_inset needs to be correctly specified. The Document Class is Article (Standard Class.) Let's get to the problem at hand. \end_layout \begin_layout Section A simple \begin_inset ERT status open \begin_layout Plain Layout \backslash sff{R} \end_layout \end_inset code chunk \end_layout \begin_layout Standard \begin_inset ERT status open \begin_layout Plain Layout <<>>= \end_layout \begin_layout Plain Layout 1+1 \end_layout \begin_layout Plain Layout @ \end_layout \end_inset \end_layout \begin_layout Standard That one ran OK. Let's create a program listing to display the complete code of the above chunk using Figure (See \begin_inset CommandInset ref LatexCommand ref reference "CodeinFigure" plural "false" caps "false" noprefix "false" \end_inset ), Table (See \begin_inset CommandInset ref LatexCommand ref reference "CodeinTable" plural "false" caps "false" noprefix "false" \end_inset ) and Algorithm floats: \end_layout \begin_layout Standard \begin_inset Float figure wide false sideways false status open \begin_layout Plain Layout \begin_inset listings inline false status open \begin_layout Plain Layout <<>>= \end_layout \begin_layout Plain Layout 1+1 \end_layout \begin_layout Plain Layout @ \end_layout \end_inset \end_layout \begin_layout Plain Layout \begin_inset Caption Standard \begin_layout Plain Layout Including the code inside a Figure \end_layout \end_inset \begin_inset CommandInset label LatexCommand label name "CodeinFigure" \end_inset \end_layout \end_inset \end_layout \begin_layout Standard \begin_inset Float table wide false sideways false status open \begin_layout Plain Layout \begin_inset listings inline false status open \begin_layout Plain Layout <<>>= \end_layout \begin_layout Plain Layout 1+1 \end_layout \begin_layout Plain Layout @ \end_layout \end_inset \end_layout \begin_layout Plain Layout \begin_inset Caption Standard \begin_layout Plain Layout Including program listing inside a Table \end_layout \end_inset \begin_inset CommandInset label LatexCommand label name "CodeinTable" \end_inset \end_layout \end_inset \end_layout \begin_layout Standard \begin_inset Float algorithm wide false sideways false status open \begin_layout Plain Layout \begin_inset listings inline false status open \begin_layout Plain Layout <<>>= \end_layout \begin_layout Plain Layout 1+1 \end_layout \begin_layout Plain Layout @ \end_layout \end_inset \end_layout \begin_layout Plain Layout \begin_inset Caption Standard \begin_layout Plain Layout Including program listing in Algorithm \end_layout \end_inset \begin_inset CommandInset label LatexCommand label name "CodeinAlgorithm" \end_inset \end_layout \end_inset \end_layout \begin_layout Standard This was the process: Insert ▷ Float ▷ Figure, Table, Algorithm whichever applicable. Then, inside the float, Insert ▷ Program Listing... I just copy-pasted the code inside the knitr chunk... Preview doesn't render the floats correctly... \end_layout \end_body \end_document
内容的提问来源于stack exchange,提问作者Diego
相关产品推荐
相关产品推荐

