Lyx/knitr中的R块后缺少PDF中的文本 [英] Missing text in PDF after R chunk in Lyx/knitr

查看:89
本文介绍了Lyx/knitr中的R块后缺少PDF中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Fedora 20 x86_64系统上将Lyx 2.1.2与knitr一起使用.我不确定如何确定knitr的版本,但是它肯定可以正常工作,通常可以提供很好的结果.但是,我看到两个问题:(1)如果我在任何块中都包含最后一个@,它将在字面上直接出现.自然,我不介意是否不必添加最后的@.无论如何,它都暗示在块的末尾.但是(2)如果我不将最后一个@包含在块3中(见下文),则块3和4之间的所有文档文本将从PDF输出中消失.

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.

在创建一个最小的示例(很抱歉之前没有发布过一个示例)时,我发现该行为的关键是在Chunk 3之后立即出现了Medium Vertical Skip.如果删除该示例,则输出文档文本很好.

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.

我看不到将文件附加到此帖子的方法,因此我将其包含在文本中.请让我知道是否有更好的方法来发布示例.此文件为minimal.lyx:

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

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

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

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

这是pdflatex的minimal.tex输出:

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}

如您所见,第一个图形的说明,小节标题和小节A1中的初始文本未出现在输出中.它只是跳到块4的输出.如果我(a)在块3的末尾添加"@"字符,或在块3之后删除MedSkip,则会在输出中恢复丢失的文本.因此,似乎有某个地方的虫子...

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!

推荐答案

简短答案:将光标放在垂直空间"的左侧,然后按回车键(请注意,您不会看到任何事情发生).然后编译您的文档.

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.

长答案: 您遇到了LyX错误 http://www.lyx.org/trac/ticket/8875 (将在LyX 2.2.0中修复).问题是,要使编织器正常工作,@本身必须位于一行上.

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.

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

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.

这样做的原因是在LyX 2.1.x块中,<<和>>的处理方式不同,因此您不必自己编写它们.要解决此问题,请删除<<和>>,然后选择保留的文本(之前位于它们之间),然后转到插入">选项"(或对第一个参数"执行Alt + A 1).

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").

能否请您打开 http://www.lyx.org/trac 上的新LyX票证并在此处发布您的最小示例?这样我们就可以解决错误的转换.

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.

这篇关于Lyx/knitr中的R块后缺少PDF中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆