2

This is what I am trying to accomplish in LyX:

  1. 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).
  2. The code that I wish to insert is this:
<<A-simple-plot>>= x<-rnorm(10) y<-rnorm(10) plot(x,y) @ 
  1. 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 
9
  • It looks like you want to use knitr, but a LaTeX "program listing" is not knitr (from what I know). Take a look at Help > Specific Manuals > Knitr. If you don't want it to be executed in LyX, just use the knitr chunk option "eval = FALSE". Commented Jan 8, 2024 at 15:35
  • Thank you for looking into this. No, I don't want to use knitr in this case: I want to be able to provide an example of a knitr chunk inside an algorithm float in a LyX document (I am aware of the modules that I need to use in LyX and the package that I need to install in R.) I just want the verbatim program listing as shown in my question. Commented Jan 9, 2024 at 12:25
  • I see. Can you post a minimal example .lyx file so I can take a look? Then ping me (use @ followed by my user name). See here for more information: wiki.lyx.org/FAQ/MinimalExample Commented Jan 9, 2024 at 20:11
  • @scottkosty it is the very first time that I try to do this. How can I upload the lyx file? Commented Jan 11, 2024 at 0:44
  • Please read the link, at the part "How do I post a .lyx file...". Commented Jan 11, 2024 at 15:55

1 Answer 1

1

@scottkosty I found a workaround that's just horrible but does the trick using a frameless box inside an algorithm float. Here's a minimal example:

#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 A horrible workaround \end_layout \begin_layout Author Diego A. Acosta M., PhD \end_layout \begin_layout Date 2024-01-15 \end_layout \begin_layout Abstract This is a horrible workaround to solve a question I asked at \begin_inset Flex URL status open \begin_layout Plain Layout https://stackoverflow.com \end_layout \end_inset where I wanted to add an algorithm float with a program listing inside with \begin_inset ERT status open \begin_layout Plain Layout \backslash textsf{R} \end_layout \end_inset knitr code not to be executed. I found that the module Rnw was interfering with what I wanted. Thus, a workaround to be able to display the whole code using an algorithm float a plain box from LyX. In this minimal working example I show how I achieved a less than optimal solution. \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 an Algorithm float using a Program Listing as in \begin_inset CommandInset ref LatexCommand ref reference "didntwork" plural "false" caps "false" noprefix "false" \end_inset : \end_layout \begin_layout Standard \begin_inset Float algorithm wide false sideways false status collapsed \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 This one didn't work \end_layout \end_inset \begin_inset CommandInset label LatexCommand label name "didntwork" \end_inset \end_layout \begin_layout Plain Layout \end_layout \end_inset \end_layout \begin_layout Standard If you run this with the Rnw module it wont work... \end_layout \begin_layout Section A very nasty workaround \end_layout \begin_layout Standard Instead of adding a Program Listing in the float, add a Frameless Box and add the code as plain text as shown in \begin_inset CommandInset ref LatexCommand ref reference "itworked" plural "false" caps "false" noprefix "false" \end_inset \end_layout \begin_layout Standard \begin_inset Float algorithm wide false sideways false status collapsed \begin_layout Plain Layout \begin_inset Box Frameless position "t" hor_pos "c" has_inner_box 1 inner_pos "t" use_parbox 0 use_makebox 0 width "100col%" special "none" height "1in" height_special "totalheight" thickness "0.4pt" separation "3pt" shadowsize "4pt" framecolor "black" backgroundcolor "none" status open \begin_layout Plain Layout <<>>= \end_layout \begin_layout Plain Layout 1+2 \end_layout \begin_layout Plain Layout @ \end_layout \end_inset \end_layout \begin_layout Plain Layout \begin_inset Caption Standard \begin_layout Plain Layout A nice algortithm \end_layout \end_inset \begin_inset CommandInset label LatexCommand label name "itworked" \end_inset \end_layout \begin_layout Plain Layout \end_layout \end_inset \end_layout \begin_layout Standard Upon running, it's OK... Like meh! but it works... \end_layout \end_body \end_document 
Sign up to request clarification or add additional context in comments.

1 Comment

I wonder what @yihui would think of this...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.