【问题标题】:Lyx/knitr chunk termination in table cells表格单元格中的 Lyx/knitr 块终止
【发布时间】:2014-12-01 05:47:55
【问题描述】:

我在 Fedora 20 上使用 Lyx 2.1.2 和 knitr(不确定如何确定版本)。当块嵌入表格单元格时,我遇到了终止 knitr 块的问题。如果我没有输入块的最后 @ 行,我会收到错误并且无法创建文档。如果我确实输入了块的最后 @ 行,它可以工作,但 @ 标记在 R 代码输出之后的表格单元格中逐字出现。

我怀疑这是我在另一篇帖子中提到的一般问题的一部分,与紧跟在 knitr 块之后的 LaTex 对象有关。

这是一个演示问题的示例 Lyx 文档:

\lyxformat 474
\begin_document
\begin_header
\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
\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 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
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\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 Standard
This is an example of a problem that arises when knitr chunks are embedded
 in Lyx table cells.
 Here is the table:
\end_layout

\begin_layout Standard
\begin_inset Tabular
<lyxtabular version="3" rows="2" columns="2">
<features rotate="0" tabularvalignment="middle">
<column alignment="center" valignment="top">
<column alignment="center" valignment="top">
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
Text
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
Chunk
\end_layout

\end_inset
</cell>
</row>
<row>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
Arbitrary Text
\end_layout

\end_inset
</cell>
<cell alignment="center" valignment="top" topline="true" bottomline="true" leftline="true" rightline="true" usebox="none">
\begin_inset Text

\begin_layout Plain Layout
\begin_inset Flex Chunk
status open

\begin_layout Plain Layout

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

\begin_layout Plain Layout

print("Hello World")
\end_layout

\begin_layout Plain Layout

@
\end_layout

\end_inset


\end_layout

\end_inset
</cell>
</row>
</lyxtabular>

\end_inset


\end_layout

\end_body
\end_document

这是 pdflatex 的 Tex 输出:

\batchmode
\makeatletter
\def\input@path{{/home/loga/bugs/cell-chunk//}}
\makeatother
\documentclass[english]{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.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

\makeatother

\usepackage{babel}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
This is an example of a problem that arises when knitr chunks are
embedded in Lyx table cells. Here is the table:

\begin{tabular}{|c|c|}
\hline 
Text &
Chunk\tabularnewline
\hline 
\hline 
Arbitrary 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}
@\tabularnewline
\hline 
\end{tabular}
\end{document}

查看 Tex 输出(从底部算起四行)@ 标记出现在 knitrout 环境之外,但在 Lyx 文件中,@ 标记位于块内,并且(至少在所见即所得术语中)显然不打算出现在文档中。

有没有办法解决这个问题,或者它是一个需要修复的错误?当然,如果我可以发布任何其他有用的数据,请告诉我。

谢谢!

【问题讨论】:

  • 好帖子!感谢您提供 MWE。一个小评论:当您提到另一个问题时,如果您链​​接到它会很有用。在这种情况下,您指的是 stackoverflow.com/questions/27182368/… 。您怀疑这与该帖子有关是正确的。

标签: knitr lyx


【解决方案1】:

这是由于以下 LyX 错误: http://www.lyx.org/trac/ticket/8875(将在 LyX 2.2.0 中修复)

您已经有一个解决方法(添加@)。您可能更喜欢的另一项工作是使用 LyX ERT(要了解有关 ERT 和 LyX 块之间差异的更多信息,请参阅Difference between ERT and LyX' custom knitr insets)。为此,删除块插入,并将光标放在它所在的表格单元格中,然后按“ctrl + l”,(或插入> TeX代码)

<<example,echo=FALSE>>=
print("Hello World")
@

我不能在代码中添加空行,但顶部和底部的 ERT 框内必须有一个空行。这样可以确保 knitr 识别出它是一个 knitr 块。

请注意,导出到 Rnw 有助于诊断这些问题。

【讨论】:

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