Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am using knitr to create inline sparkline-esque graphics. However, when knitr inserts images that it generates with R, it add a blank line, which forces LaTeX to make a new paragraph.

That is, if I have text like this (not actual knitr code...):

Lorem ipsum dolor sit amet KNITR_FIGURE_HERE Ut enim ad minim veniam, quis nostrud exercitation.

It will generate TeX output like this

Lorem ipsum dolor sit amet
\begin{knitrout}
\includegraphics{path_to_figure} 

\end{knitrout}

Ut enim ad minim veniam, quis nostrud exercitation.

Which then forces the remaining text in the paragraph into a new paragraph. If I manually remove the blank line in the knitrout environment, it works inline as expected.

Hacking the knitr output to remove the blank line is non-trivial, so I'm wondering if there's a TeX command that can force a paragraph to be part of the previous one, regardless of new lines. For example, assuming \keepwithprevious is this hypothetical command...

Lorem ipsum dolor sit amet.

\keepwithprevious Ut enim ad minim veniam, quis nostrud exercitation.

...would result in this when typeset:

Lorem ipsum dolor sit amet. Ut enim ad minim veniam, quis nostrud exercitation.

Is there a way to do this in TeX?

share|improve this question
    
In the case of knitr, does tex.stackexchange.com/a/55918/24449 achieve the desired behavior? –  e-birk Feb 12 '13 at 17:10
    
Ooh, \gobblepars looks promising, as does \eatpar at tex.stackexchange.com/a/24790/11851. I just can't get either to work right now... –  Andrew Feb 12 '13 at 17:34
    
The 'samepage' environment in an answer here: tex.stackexchange.com/questions/78226/… may help. You still get a new paragraph, but no page break. –  Ethan Bolker Feb 12 '13 at 18:20
add comment

2 Answers

up vote 4 down vote accepted

Here is an approach not really involving TeX. The knitr code that generates the TeX output was not contained in the question. So I made up the following minimal example knitr-inline.Rnw that creates a similar TeX output. Furthermore, my knitr code automatically removes the empty line at the end of the "knitrout" environment. The idea is to replace "newline+newline+\end{knitrout}" by "newline+\end{knitrout}" using gsub at the document hook in R.

\documentclass{article}
\begin{document}
<<setup, echo=FALSE>>=
knit_hooks$set(document = function(x) {
  gsub('(\n\n\\\\end\\{knitrout\\}[\n]+)', '\n\\\\end\\{knitrout\\}\n', paste(x, collapse = '\n'))
})
@

Here is some text followed by a picture
<<echo=FALSE, out.width="11mm">>=
r <- rlnorm(8)
pie(r)
@
and the text continues for another line.

This is a new paragraph, which is indented as usual.
\end{document}

Running library(knitr);knitr(knitr-inline.Rnw) in R creates knitr-inline.tex. Compling it with "pdflatex" gives:

compiled knitr-inline.tex

share|improve this answer
    
This is the simplest solution, sans TeX. Yuhui does warn (in an e-mail I got from him) that using gsub() like that can potentially result in unexpected output, mostly if you forget to put a newline after @ in the knitr block. –  Andrew Feb 14 '13 at 0:50
add comment

Below example works for me (based on Command that gobbles all following empty lines)

\documentclass{article}
\usepackage{color}
\newenvironment{knitrout}{}{}  %just a dummy environment
\makeatletter
\newcommand\gobblepars{%
    \@ifnextchar\par%
        {\expandafter\gobblepars\@gobble}%
        {}}
\makeatother

\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam
lobortis facilisis%
\begin{knitrout}
\color{red} sem. Nullam\gobblepars

\end{knitrout}
 nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Etiam
lobortis facilisis 
sem. Nullam 
nec mi et neque pharetra sollicitudin. Praesent imperdiet mi nec ante.
\end{document}

Note the spacing. It looks identical with and without environment. compiled example document

share|improve this answer
    
Can \gobblepars be placed outside the \begin{knitrout} environment? –  Andrew Feb 12 '13 at 18:22
    
Unfortunately not. \gobblepars eats up only the start of a new paragraph that follows immediately after it. - Could you add the 'knitr' code with which you generate the TeX code in your question? The \gobblepars needs to be inserted there somehow... –  e-birk Feb 12 '13 at 20:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.