rstudio / rticles

LaTeX Journal Article Templates for R Markdown
https://pkgs.rstudio.com/rticles/
1.47k stars 519 forks source link

Template crashes when using fig.align or out.width #300

Closed EddieItelman closed 4 years ago

EddieItelman commented 4 years ago

Sample:

---
title: my title
author:
  - name: Edward Itelman
    affiliation: a
  - name: Second Equal
    affiliation: b

address:
  - code: a
    address: Department, Street, City, State, Zip
  - code: b
    address: Department, Street, City, State, Zip

journal: "An awesome journal"
date: "`r Sys.Date()`"

documentclass: article

fontsize: 11pt

geometry: margin=1in

header-includes: #allows you to add in your own Latex packages
- \usepackage{float} #use the 'float' package
- \floatplacement{figure}{H} #make every figure with caption = h
- \usepackage{caption}
- \usepackage{chngcntr}
- \usepackage{graphicx}

graphics: yes

output:
  rticles::elsevier_article
---

```{R fig2, echo = F, fig.cap = "Figure1", fig.pos = "h", fig.align = 'center', out.width = '20%'}
plot(mtcars$mpg)```

Results in the following error:

! Paragraph ended before \Gin@iii was complete.
<to be read again> 
                   \par 
l.153 

Error: LaTeX failed to compile RMD_6.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See RMD_6.log for more info.
Execution halted

If you remove the fig.align = 'center' and out.width = '20%' - Works like a charm

cderv commented 4 years ago

I think this is because the elsevier template assumes that all images will have \max.width https://github.com/rstudio/rticles/blob/504c5eb3e5d0848f6b7fb875a51af64f94003349/inst/rmarkdown/templates/elsevier_article/resources/template.tex#L80-L89

The code above means that \includegraphics will be in fact in this template \includegraphics[width=\maxwidth]

That means you can't set up another width attributes for \includegraphics as it has already been set by default. Only out.width causes this latex error by the way. (because it add width = a second time)

I am not an expert in elsevier template so I don't know why this has been added and if

  1. we should adapt the template
  2. warn (if possible) that out.width can't be use with this template
EddieItelman commented 4 years ago

Hi!

Thanks for the input! I also observed the behavior with fig.align = 'center'. Were you not able to reproduce? If not I can provide another code sample

On Sun, 19 Jul 2020, 13:00 Christophe Dervieux, notifications@github.com wrote:

I think this is because the elsevier template assumes that all images will have \max.width

https://github.com/rstudio/rticles/blob/504c5eb3e5d0848f6b7fb875a51af64f94003349/inst/rmarkdown/templates/elsevier_article/resources/template.tex#L80-L89

That means you can't set up another width attributes for \includegraphics as it has already been set by default. Only out.width causes this latex error by the way.

I am not an expert in elsevier template so I don't know if

  1. we should adapt the template
  2. warn (if possible) that out.width can't be use with this template

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rstudio/rticles/issues/300#issuecomment-660619340, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOLHV7IW7WOXTRNGR32NTIDR4K74BANCNFSM4O74WQXQ .

cderv commented 4 years ago

I also observed the behavior with fig.align = 'center'. Were you not able to reproduce? If not I can provide another code sample

The document renders on my side to PDF if I leave fig.align and remove out.width

EddieItelman commented 4 years ago

Interesting. For me it doesn't with the same error. What version of r/rmd/pandoc are you using?

On Sun, 19 Jul 2020, 14:14 Christophe Dervieux, notifications@github.com wrote:

I also observed the behavior with fig.align = 'center'. Were you not able to reproduce? If not I can provide another code sample

The document renders on my side to PDF if I leave fig.align and remove out.width

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rstudio/rticles/issues/300#issuecomment-660627802, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOLHV7PZD6BPORVZ6LAU2R3R4LIR3ANCNFSM4O74WQXQ .

cderv commented 4 years ago

With R 3.6.3

packageVersion("rticles")
#> [1] '0.14'
packageVersion("knitr")
#> [1] '1.29'
packageVersion("rmarkdown")
#> [1] '2.3.2'
rmarkdown::pandoc_version()
#> [1] '2.10'
tinytex::tlmgr("--version")
#> tlmgr --version
#> tlmgr revision 55369 (2020-06-01 02:32:00 +0200)
#> tlmgr using installation: C:/Users/chris/AppData/Roaming/TinyTeX
#> TeX Live (https://tug.org/texlive) version 2020

Created on 2020-07-19 by the reprex package (v0.3.0.9001)

But it also works with pandoc 2.7.3

EddieItelman commented 4 years ago

You are right, just did a re-test and it worked with fig.align.

What do you think can be done with regards to the fig.out matter?

On Sun, Jul 19, 2020 at 2:28 PM Christophe Dervieux < notifications@github.com> wrote:

With R 3.6.3

packageVersion("rticles")#> [1] '0.14' packageVersion("knitr")#> [1] '1.29' packageVersion("rmarkdown")#> [1] '2.3.2'rmarkdown::pandoc_version()#> [1] '2.10'tinytex::tlmgr("--version")#> tlmgr --version

Created on 2020-07-19 by the reprex package https://reprex.tidyverse.org (v0.3.0.9001)

But it also works with pandoc 2.7.3

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rstudio/rticles/issues/300#issuecomment-660629510, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOLHV7J3HEB6FRYNQKUD6RTR4LKHFANCNFSM4O74WQXQ .

cderv commented 4 years ago

What do you think can be done with regards to the fig.out matter?

It seems this template does not allow to modify the fig width. I don't know if this is part of the elsevier article specification this template is from. Otherwise, maybe it can be modify.

Is modifying the out.width part of an article you need to submit ?

For now, as a workaround,

---
title: my title
author:
  - name: Edward Itelman
    affiliation: a
  - name: Second Equal
    affiliation: b
address:
  - code: a
    address: Department, Street, City, State, Zip
  - code: b
    address: Department, Street, City, State, Zip
journal: "An awesome journal"
date: "`r Sys.Date()`"
documentclass: article
fontsize: 11pt
geometry: margin=1in
header-includes: #allows you to add in your own Latex packages
- \usepackage{float} #use the 'float' package
- \floatplacement{figure}{H} #make every figure with caption = h
- \usepackage{caption}
- \usepackage{chngcntr}
- \usepackage{graphicx}
graphics: yes
output: rticles::elsevier_article
---

<!-- use the original \includegraphics command saved in the template -->
\let\saveincludegraphics\includegraphics
\renewcommand{\includegraphics}{\Oldincludegraphics}

```{R fig2, echo = F, fig.cap = "Figure1", fig.pos = "h", fig.align = 'center', out.width = '20%'}
plot(mtcars$mpg)

\renewcommand{\includegraphics}{\saveincludegraphics}

plot(mtcars$mpg)

You can even apply that as a chunk hook so that those lines are added before and after a chunk with `out.width` used. 

<details><summary> See example</summary>

````md
---
title: my title
author:
  - name: Edward Itelman
    affiliation: a
  - name: Second Equal
    affiliation: b
address:
  - code: a
    address: Department, Street, City, State, Zip
  - code: b
    address: Department, Street, City, State, Zip
journal: "An awesome journal"
date: "`r Sys.Date()`"
documentclass: article
fontsize: 11pt
geometry: margin=1in
header-includes: #allows you to add in your own Latex packages
- \usepackage{float} #use the 'float' package
- \floatplacement{figure}{H} #make every figure with caption = h
- \usepackage{caption}
- \usepackage{chngcntr}
- \usepackage{graphicx}
graphics: yes
output: rticles::elsevier_article
---

```{r, include = FALSE}
#  \ needs to be escaped to \\ so that it is printed as \ in latex document
knitr::knit_hooks$set(
  out.width = function(before, options, envir) {
    if(before) {
      return("\\let\\saveincludegraphics\\includegraphics\n\\renewcommand{\\includegraphics}{\\Oldincludegraphics}")
    }
    "\\renewcommand{\\includegraphics}{\\saveincludegraphics}"
  }
)
plot(mtcars$mpg)
plot(mtcars$mpg)


</details>
EddieItelman commented 4 years ago

Hi,

It's not for a specific submission more building my own template based on this one which was originally very close to my needs but needed some tweaks (anyway simpler the writing my own), this is why I also changed the way it handles picture placements.

Your solution should do the trick, thank you very much for your help!!

On Sun, 19 Jul 2020, 23:27 Christophe Dervieux, notifications@github.com wrote:

What do you think can be done with regards to the fig.out matter?

It seems this template does not allow to modify the fig width. I don't know if this is part of the elsevier article specification this template is from. Otherwise, maybe it can be modify.

Is modifying the out.width part of an article you need to submit ?

For now, as a workaround,

  • You can copy the template, modify the part where they define the new command then use the template argument in pdf_document to use it.
  • You could also modify the command back inside the document itself. It is a bit of a hack but it seems to work. Example:

title: my title author:

  • name: Edward Itelman affiliation: a
  • name: Second Equal affiliation: b address:
  • code: a address: Department, Street, City, State, Zip
  • code: b address: Department, Street, City, State, Zip journal: "An awesome journal" date: "r Sys.Date()" documentclass: article fontsize: 11pt geometry: margin=1in header-includes: #allows you to add in your own Latex packages
    • \usepackage{float} #use the 'float' package
    • \floatplacement{figure}{H} #make every figure with caption = h
    • \usepackage{caption}
    • \usepackage{chngcntr}
    • \usepackage{graphicx} graphics: yes output: rticles::elsevier_article

\let\saveincludegraphics\includegraphics \renewcommand{\includegraphics}{\Oldincludegraphics}

plot(mtcars$mpg)

\renewcommand{\includegraphics}{\saveincludegraphics}

plot(mtcars$mpg)

You can even apply that as a chunk hook so that those lines are added before and after a chunk with out.width used. See example


title: my title author:

  • name: Edward Itelman affiliation: a
  • name: Second Equal affiliation: b address:
  • code: a address: Department, Street, City, State, Zip
  • code: b address: Department, Street, City, State, Zip journal: "An awesome journal" date: "r Sys.Date()" documentclass: article fontsize: 11pt geometry: margin=1in header-includes: #allows you to add in your own Latex packages
    • \usepackage{float} #use the 'float' package
    • \floatplacement{figure}{H} #make every figure with caption = h
    • \usepackage{caption}
    • \usepackage{chngcntr}
    • \usepackage{graphicx} graphics: yes output: rticles::elsevier_article

#  \ needs to be escaped to \\ so that it is printed as \ in latex document
knitr::knit_hooks$set(
  out.width = function(before, options, envir) {
    if(before) {
      return("\\let\\saveincludegraphics\\includegraphics\n\\renewcommand{\\includegraphics}{\\Oldincludegraphics}")
    }
    "\\renewcommand{\\includegraphics}{\\saveincludegraphics}"
  }
)
plot(mtcars$mpg)
plot(mtcars$mpg)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rstudio/rticles/issues/300#issuecomment-660703655, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOLHV7MUZ7PYIYZGTXPGNWTR4NJJRANCNFSM4O74WQXQ .

cderv commented 4 years ago

It's not for a specific submission more building my own template based on this one which was originally very close to my needs but needed some tweaks (anyway simpler the writing my own), this is why I also changed the way it handles picture placements.

Then you can just not use these lines in your template https://github.com/rstudio/rticles/blob/504c5eb3e5d0848f6b7fb875a51af64f94003349/inst/rmarkdown/templates/elsevier_article/resources/template.tex#L88-L89 They are the one which redefine the command \includegraphics and make the out.width chunk option not working with this.

EddieItelman commented 4 years ago

Also a good option. Thanks mate!

On Sun, 19 Jul 2020, 23:52 Christophe Dervieux, notifications@github.com wrote:

It's not for a specific submission more building my own template based on this one which was originally very close to my needs but needed some tweaks (anyway simpler the writing my own), this is why I also changed the way it handles picture placements.

Then you can just not use this lines in your template

https://github.com/rstudio/rticles/blob/504c5eb3e5d0848f6b7fb875a51af64f94003349/inst/rmarkdown/templates/elsevier_article/resources/template.tex#L88-L89 They are the one which redefine the command \includegraphics

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rstudio/rticles/issues/300#issuecomment-660707370, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOLHV7IHQTRGGIXCDO7ZK3DR4NMINANCNFSM4O74WQXQ .

yihui commented 4 years ago

I feel it doesn't make sense to force width=\maxwidth in the template. @cderv Could you find out the author of this template and see if he/she could remove this constraint? Thanks! If you don't hear back in a few weeks, I'd like to remove it by ourselves.

cderv commented 4 years ago

@cboettig I think you contributed this template. What are you thoughts on this one ? Could you the maxwidth constraint be removed from the template ? Is it part of the requirement for this journal ?

This conflicts with out.width chunk option which tries to set also the width.

Thank you!

cboettig commented 4 years ago

@yihui @cderv thanks, yes, please go ahead and patch this as you see fit! My contribution was merely a starting point!

yihui commented 4 years ago

Okay. Let's get rid of the hard-coded width. Thanks!

github-actions[bot] commented 3 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.