New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunk option `fig.align` causes image tags to be created in .html #611

Closed
rnorberg opened this Issue Sep 24, 2013 · 7 comments

Comments

4 participants
@rnorberg

rnorberg commented Sep 24, 2013

I frequently write Rmd files in RStudio and then knit them using the built in knitHTML button. Occasionally I like to produce something other than an html file from my code though. For this, I use pandoc() to convert the markdown file that is produced with each click of the knitHTML button into some other document type (mostly .docx files).

I always set several chunk options globally at the beginning of each Rmd document I create, like so:

opts_chunk$set(comment=NA, fig.height=8, fig.width=7, fig.align='center')

This always resulted in normal markdown-style image tags in the knitted .md file. An example:

![plot of chunk BoxPlots](Some_Path/BoxPlots.png) 

I noticed this was no longer the case after updating to the most recent version of knitr because when I called pandoc() to convert my .md file to a .docx, no images appeared in the resulting Word file. After some exploration I found that my images were now html tagged in the .md file. An example:

<img src="Some_Path/BoxPlots.png" title="Box Plot" alt="Box Plot" style="display: block; margin: auto;" />

This is what was causing my images to no longer appear in my converted Word files. I traced this back to the chunk option fig.align. If I remove this setting from my global chunk settings at the beginning of the document, all of my image tags are as they should be and the .md file converts to .docx perfectly. If I set the option for just one chunk, the figure tag(s) produced by that chunk are .html tags and all of the other figure tags are markdown-style.

I realize that setting this option makes no difference in the resulting .md file, so you might make the argument "Why are you using it anyway?", but I don't ignore the .html file produced. I'd like to be able to specify how the .html file appears and still be able to convert the .md file to some other file format if need be.

Also, I had code that "worked" and after installing the new package, it "broke", so I thought I might help some others having this issue.

Thanks for the great package!

@ramnathv

This comment has been minimized.

Show comment
Hide comment
@ramnathv

ramnathv Sep 24, 2013

Contributor

Certain chunk options will make knitr switch the plot output to html as opposed to plain markdown. You can always modify default behavior using plots. In this case, the options fig.align and out.width or out.height will cause knitr to output image tags, since these options cannot be accommodated in markdown.

The fig.align portion of switching was incorporated in a recent version, which is why you did not face this issue earlier.

Contributor

ramnathv commented Sep 24, 2013

Certain chunk options will make knitr switch the plot output to html as opposed to plain markdown. You can always modify default behavior using plots. In this case, the options fig.align and out.width or out.height will cause knitr to output image tags, since these options cannot be accommodated in markdown.

The fig.align portion of switching was incorporated in a recent version, which is why you did not face this issue earlier.

@rnorberg

This comment has been minimized.

Show comment
Hide comment
@rnorberg

rnorberg Sep 24, 2013

Ok, thanks! How might I change the default behavior? If I do this, will the chunk option still affect the .html file created?

rnorberg commented Sep 24, 2013

Ok, thanks! How might I change the default behavior? If I do this, will the chunk option still affect the .html file created?

@ramnathv

This comment has been minimized.

Show comment
Hide comment
@ramnathv

ramnathv Sep 24, 2013

Contributor

I have described below, how you can go about modifying the defaults. However, long story short, it is a lot easier no using the chunk options that trigger html output, if you don't want img tags in your output.


So, here is the hook that is used to render a plot in md. So you see that it switches to html syntax, whenever the chunk options out.width, out.height or out.extra are specified, OR you modify the default fig.align option. If you always want to use the md syntax, you can remove all those checks and simplify the hook (see below). You will also have to explicitly set hook_plot_md2 as the plotting hook by calling knit_hooks$set(plot = hook_plot_md2).

hook_plot_md = function(x, options) {
  if (options$fig.show == 'animate') return(hook_plot_html(x, options))

  base = opts_knit$get('base.url') %n% ''
  cap = .img.cap(options)

  if(is.null(w <- options$out.width) & is.null(h <- options$out.height) &
    is.null(s <- options$out.extra) & options$fig.align == 'default') {
    return(sprintf('![%s](%s%s) ', cap, base, .upload.url(x)))
  }
  # use HTML syntax <img src=...>
  .img.tag(.upload.url(x), w, h, cap, c(s, sprintf(
    'style="display: block; margin: %s;"', switch(
      options$fig.align, left = 'auto auto auto 0', center = 'auto',
      right = 'auto 0 auto auto')
  )))
}

Modified Plot Hook to use Md syntax always.

hook_plot_mds = function(x, options) {
  if (options$fig.show == 'animate') return(hook_plot_html(x, options))

  base = opts_knit$get('base.url') %n% ''
  cap = .img.cap(options)
  return(sprintf('![%s](%s%s) ', cap, base, .upload.url(x)))  
}
Contributor

ramnathv commented Sep 24, 2013

I have described below, how you can go about modifying the defaults. However, long story short, it is a lot easier no using the chunk options that trigger html output, if you don't want img tags in your output.


So, here is the hook that is used to render a plot in md. So you see that it switches to html syntax, whenever the chunk options out.width, out.height or out.extra are specified, OR you modify the default fig.align option. If you always want to use the md syntax, you can remove all those checks and simplify the hook (see below). You will also have to explicitly set hook_plot_md2 as the plotting hook by calling knit_hooks$set(plot = hook_plot_md2).

hook_plot_md = function(x, options) {
  if (options$fig.show == 'animate') return(hook_plot_html(x, options))

  base = opts_knit$get('base.url') %n% ''
  cap = .img.cap(options)

  if(is.null(w <- options$out.width) & is.null(h <- options$out.height) &
    is.null(s <- options$out.extra) & options$fig.align == 'default') {
    return(sprintf('![%s](%s%s) ', cap, base, .upload.url(x)))
  }
  # use HTML syntax <img src=...>
  .img.tag(.upload.url(x), w, h, cap, c(s, sprintf(
    'style="display: block; margin: %s;"', switch(
      options$fig.align, left = 'auto auto auto 0', center = 'auto',
      right = 'auto 0 auto auto')
  )))
}

Modified Plot Hook to use Md syntax always.

hook_plot_mds = function(x, options) {
  if (options$fig.show == 'animate') return(hook_plot_html(x, options))

  base = opts_knit$get('base.url') %n% ''
  cap = .img.cap(options)
  return(sprintf('![%s](%s%s) ', cap, base, .upload.url(x)))  
}
@rnorberg

This comment has been minimized.

Show comment
Hide comment
@rnorberg

rnorberg Sep 24, 2013

Perfect! I'll just set the default alignment to 'center'. Thanks so much!

rnorberg commented Sep 24, 2013

Perfect! I'll just set the default alignment to 'center'. Thanks so much!

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Sep 24, 2013

Owner

Thanks, @ramnathv.

@rnorberg Figure alignment does not make sense to markdown, since markdown does not support it. When you set fig.align='center', I have to switch to the HTML syntax to satisfy you. It is good (consistency with other document formats) and bad (makes markdown ugly).

It is totally fine to modify the default plot hook, and just remember when you modify it, it is yours! :) I would rather not set fig.align at all. Or make the modification minimal so you do not have to maintain the monster:

plot_hook = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
  options$fig.align = 'default'  # whatever you set, I reset it
  plot_hook(x, options)
})
Owner

yihui commented Sep 24, 2013

Thanks, @ramnathv.

@rnorberg Figure alignment does not make sense to markdown, since markdown does not support it. When you set fig.align='center', I have to switch to the HTML syntax to satisfy you. It is good (consistency with other document formats) and bad (makes markdown ugly).

It is totally fine to modify the default plot hook, and just remember when you modify it, it is yours! :) I would rather not set fig.align at all. Or make the modification minimal so you do not have to maintain the monster:

plot_hook = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) {
  options$fig.align = 'default'  # whatever you set, I reset it
  plot_hook(x, options)
})

@yihui yihui closed this in 6b40a66 Sep 24, 2013

@froh

This comment has been minimized.

Show comment
Hide comment
@froh

froh Dec 8, 2014

FWIW, pandoc is now working on an extension to allow for width and height tags (and and id and a class tag) for images. Should I open a new issue to get that supported, once it's there?

froh commented Dec 8, 2014

FWIW, pandoc is now working on an extension to allow for width and height tags (and and id and a class tag) for images. Should I open a new issue to get that supported, once it's there?

@yihui

This comment has been minimized.

Show comment
Hide comment
@yihui

yihui Dec 8, 2014

Owner

@froh You can certainly open a new issue if you think @jgm will eventually agrees to support it in Pandoc. Please also note that the original problem in this issue has been "solved": you can convert R Markdown to Word documents when you have set fig.align (it is just that fig.align will be ignored for Word).

Owner

yihui commented Dec 8, 2014

@froh You can certainly open a new issue if you think @jgm will eventually agrees to support it in Pandoc. Please also note that the original problem in this issue has been "solved": you can convert R Markdown to Word documents when you have set fig.align (it is just that fig.align will be ignored for Word).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment