Missing text in PDF after R chunk in Lyx/knitr

410 views Asked by At

I'm using Lyx 2.1.2 with knitr on a Fedora 20 x86_64 system. I'm not sure how to determine the version of knitr, but it is certainly working, generally providing very good results. However I see two problems: (1) If I include the final @ in any of the chunks, it appears literally in the text. Naturally I don't mind if I don't have to add the final @. It's implied by the end of the chunk, anyway. However (2) if I do NOT include the final @ in Chunk 3 (see below), then all of the document text between Chunks 3 and 4 vanishes from the PDF output.

In working to create a minimal example (sorry for not posting one before), I find that the key to the behaviour is the presence of the Medium Vertical Skip immediately after Chunk 3. If I delete that, the document text is output just fine.

I don't see a way to attach files to this post, so I'll include them in the text. Just let me know if there's a better way to post examples. This file is minimal.lyx:

#LyX 2.1 created this file. For more info see http://www.lyx.org/
\lyxformat 474
\begin_document
\begin_header
\textclass article
\use_default_options true
\begin_modules
knitr
\end_modules
\maintain_unincluded_children false
\language american
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_math auto
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize a4paper
\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
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 2
\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 Standard
\begin_inset Flex Chunk
status open

\begin_layout Plain Layout

<<import-external-code,echo=FALSE>>=
\end_layout

\begin_layout Plain Layout

read_chunk('mini.R')
\end_layout

\end_inset


\end_layout

\begin_layout Section
Section A
\end_layout

\begin_layout Standard
Some text
\end_layout

\begin_layout Standard
\begin_inset Flex Chunk
status open

\begin_layout Plain Layout

<<Mini1,echo=FALSE>>=
\end_layout

\end_inset


\begin_inset VSpace medskip
\end_inset


\end_layout

\begin_layout Standard
Explanation of first graph
\end_layout

\begin_layout Subsection
Subsection A1
\end_layout

\begin_layout Standard
Some text
\end_layout

\begin_layout Standard
\begin_inset Flex Chunk
status open

\begin_layout Plain Layout

<<Mini2,echo=FALSE>>=
\end_layout

\end_inset


\end_layout

\begin_layout Standard
Explanation of second graph
\end_layout

\end_body
\end_document

The referenced R code (mini.R) is trivial:

# ---- Mini1
print("Hello World")
# ---- Mini2
print("Where did the explanation of the first graph go?")

This is the minimal.tex output from pdflatex:

\batchmode
\makeatletter
\def\input@path{{/home/loga/bug//}}
\makeatother
\documentclass[a4paper,twoside,american]{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%

\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
 \def\at@end@of@kframe{}%
 \ifinner\ifhmode%
  \def\at@end@of@kframe{\end{minipage}}%
  \begin{minipage}{\columnwidth}%
 \fi\fi%
 \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
 \colorbox{shadecolor}{##1}\hskip-\fboxsep
     % There is no \\@totalrightmargin, so:
     \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
 \MakeFramed {\advance\hsize-\width
   \@totalleftmargin\z@ \linewidth\hsize
   \@setminipage}}%
 {\par\unskip\endMakeFramed%
 \at@end@of@kframe}
\makeatother

\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX

\usepackage{alltt}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
\pdfpageheight\paperheight
\pdfpagewidth\paperwidth


\makeatother

\usepackage{babel}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}




\section{Section A}

Some text


\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
## [1] "Hello World"
\end{verbatim}
\end{kframe}
\end{knitrout}

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
## [1] "Where did the explanation of the first graph go?"
\end{verbatim}
\end{kframe}
\end{knitrout}

Explanation of second graph
\end{document}

As you can see, the explanation of the first graph, the subsection heading, and the initial text in subsection A1 do not appear in the output. It just jumps to the output from Chunk 4. The missing text is restored in the output if I either (a) add an '@' character to the end of Chunk 3, or delete the MedSkip after Chunk 3. So it seems there is a bug in there somewhere...

Just let me know if there's anything else that would help!

1

There are 1 answers

0
scottkosty On

Short answer: put your cursor just to the left of "Vertical Space" and press return (note that you will not see anything appear to happen). Then compile your document.

Long answer: You've come across LyX bug http://www.lyx.org/trac/ticket/8875 (which will be fixed in LyX 2.2.0). The problem is that for knitr to work correctly, the @ needs to be on a line by itself.

Also, your document is not converted correctly to the LyX 2.1.x format. You can see this if you export your document to .Rnw. You get something weird like:

<<>>=
<<import-external-code,echo=FALSE>>=
read_chunk('mini.R')
@

This does not seem to affect the document output. I'm not sure why. I guess that a second <<>>= overwrites the first. But I think it would be best to clean it up.

The reason for this is that in LyX 2.1.x chunks, the << and >> are handled differently, so you should not have to write them yourself. To fix this, delete << and >> and select the text that remains (which was in-between them before) and go to Insert > Options (or do Alt + A 1, for "first argument").

Can you please open a new LyX ticket at http://www.lyx.org/trac and post your minimal example there? That way we could fix the incorrect conversion.