【问题标题】:Missing text in PDF after R chunk in Lyx/knitrLyx / knitr中的R块后PDF中缺少文本
【发布时间】:2014-11-28 05:21:53
【问题描述】:

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

在创建一个最小示例(抱歉之前没有发布)时,我发现行为的关键是在块 3 之后立即出现中等垂直跳过。如果我删除它,则输出文档文本很好。

我看不到将文件附加到此帖子的方法,因此我将它们包含在文本中。只要让我知道是否有更好的方法来发布示例。这个文件是最小的.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 的最小输出:

\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,则在输出中恢复丢失的文本。所以似乎有某处的错误...

如果还有什么可以帮助的,请告诉我!

【问题讨论】:

  • 请发布both .lyx 最小示例,以及导出到 LaTeX (pdflatex) 时获得的 .tex 文件。请参阅此处了解更多信息:wiki.lyx.org/FAQ/MinimalExample
  • 我已经更新了帖子以包含一个最小的示例,但我现在认为这是一个更普遍的 Lyx 问题的表现,该问题与紧接在块之后出现的对象有关。在上面的例子中,这是一个垂直跳过,但是如果你在表格单元格中嵌入一个块,也会出现同样的现象。我将添加另一篇文章以说明这种情况。
  • 感谢 MWE。你说得对,它与垂直空间有关。
  • 顺便说一句,欢迎来到 Stackoverflow!

标签: knitr lyx


【解决方案1】:

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

长答案: 您遇到了 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')
@

这似乎不会影响文档输出。我不确定为什么。我猜第二个 >= 会覆盖第一个。但我认为最好把它清理干净。

原因是在 LyX 2.1.x 块中,> 的处理方式不同,因此您不必自己编写它们。要解决此问题,请删除 > 并选择剩余的文本(之前在它们之间)并转到插入 > 选项(或为“第一个参数”执行 Alt + A 1)。

能否请您在http://www.lyx.org/trac 开一张新的 LyX 票并在那里发布您的最小示例?这样我们就可以修复不正确的转换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2012-09-09
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多