This is what I am trying to accomplish in LyX:
- I want to insert this piece of code as a program listing by using an Algorithm Float (Insert > Float > Algorithm) and then a Program Lisiting inside the Algorithm float (Insert > Program Listing).
- The code that I wish to insert is this:
<<A-simple-plot>>= x<-rnorm(10) y<-rnorm(10) plot(x,y) @
- The output that I get after compiling it is:
\ begin { knitrout } \ d e f i n e c o l o r { shadecolor }{ rgb }{0.969 , 0.969 , 0.969}\ color { f g c o l o r }\ begin {kframe} \ begin { a l l t t } \ hlstd {x}\hlkwb{<=}\hlkwd{rnorm}\ hlstd {(}\hlnum{10}\ hlstd {)} \ hlstd {y}\hlkwb{<=}\hlkwd{rnorm}\ hlstd {(}\hlnum{10}\ hlstd {)} \hlkwd{ plot }\ hlstd {(x , y)} \end{ a l l t t } \end{kframe} \ includegraphics [ width=\maxwidth ]{ f i g u r e /unnamed=chunk=3=1} \end{ knitrout }
This is clearly not what I want... I haven't checked the Verbatim package...
Can somebody please tell me how to fix this?
Update: Minimal Working Example @scottkosty
#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 textsf{R} \end_layout \end_inset and executing \begin_inset ERT status open \begin_layout Plain Layout \backslash textsf{R} \end_layout \end_inset code chunks and showing a program listing of an \begin_inset ERT status open \begin_layout Plain Layout \backslash textsf{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 textsf{R} \end_layout \end_inset . The path \begin_inset ERT status open \begin_layout Plain Layout \backslash textsf{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 textsf{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
@followed by my user name). See here for more information: wiki.lyx.org/FAQ/MinimalExample