在Lyx / knitr中的R chunk之后缺少PDF格式的文本

时间:2014-11-28 05:21:54

标签: knitr lyx

我在Fedora 20 x86_64系统上使用带有knitr的Lyx 2.1.2。我不确定如何确定knitr的版本,但它确实有效,通常提供非常好的结果。但是我看到两个问题:(1)如果我在任何一个块中包含最终的@,它就会出现在文本中。当然,我不介意我不必添加最终@。无论如何,它隐藏在大块的末尾。但是(2)如果我没有在Chunk 3中包含最终的@(见下文),那么Chunks 3和4之间的所有文档文本都会从PDF输出中消失。

在努力创建一个最小的例子(抱歉不发布之前的一个)时,我发现该行为的关键是在Chunk 3之后立即出现Medium Vertical Skip。如果我删除它,则输出文档文本很好。

我没有看到将文件附加到此帖子的方法,因此我将其包含在文本中。如果有更好的方式发布示例,请告诉我。这个文件是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

引用的R代码(mini.R)很简单:

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

这是pdflatex的minimal.tex输出:

\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}

如您所见,第一个图表,子部分标题和A1小节中的初始文本的说明不会出现在输出中。它只是跳转到Chunk 4的输出。如果我要么(a)在Chunk 3的末尾添加一个'@'字符,或者在Chunk 3之后删除MedSkip,那么丢失的文本将在输出中恢复。所以似乎有那里有一个小虫...

请告诉我是否还有其他任何有用的方法!

1 个答案:

答案 0 :(得分:1)

简答:将光标放在&#34; Vertical Space&#34;的左侧。并按返回(请注意,您不会看到任何事情发生)。然后编译你的文件。

答案很长: 您遇到了LyX错误http://www.lyx.org/trac/ticket/8875(将在LyX 2.2.0中修复)。问题是,为了使knitr正常工作,@需要自己就行了。

此外,您的文档未正确转换为LyX 2.1.x格式。如果将文档导出为.Rnw,则可以看到此信息。你会得到一些奇怪的东西:

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

这似乎不会影响文档输出。我不确定为什么。我想第二个&lt;&lt;&gt;&gt; =会覆盖第一个。但我认为最好将其清理干净。

这是因为在LyX 2.1.x块中,&lt;&lt;和&gt;&gt;处理方式不同,所以你不必自己写。要解决此问题,请删除&lt;&lt;和&gt;&gt;并选择剩余的文本(之前介于它们之间)并转到Insert&gt;选项(或执行Alt + A 1,对于&#34;第一个参数&#34;)。

你可以在http://www.lyx.org/trac打开一张新的LyX门票并在那里发布你的最小例子吗?这样我们就可以修复错误的转换。