Skip to content
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

fig.alt cause figure to disappear in word_document #1965

Closed
cderv opened this issue Feb 11, 2021 · 4 comments · Fixed by #1966
Closed

fig.alt cause figure to disappear in word_document #1965

cderv opened this issue Feb 11, 2021 · 4 comments · Fixed by #1966
Assignees
Labels
bug Bugs

Comments

@cderv
Copy link
Collaborator

cderv commented Feb 11, 2021

There is something not currently working as expected because the plot does not show in Word but shows in HTML

On windows

dir.create(tmp_dir <- tempfile())
owd <- setwd(tmp_dir)
xfun::write_utf8(c(
  "---",
  "title: test",
  "---",
  "",
  '```{r, fig.cap="A plot", fig.alt="Test"}',
  "plot(mtcars)",
  "```",
  ""
), "test.Rmd")
html <- rmarkdown::render("test.Rmd", "html_document")
browseURL(html)
word <- rmarkdown::render("test.Rmd", "word_document")
shell.exec(word)

setwd(owd)
unlink(tmp_dir, recursive = TRUE)
@cderv cderv added the bug Bugs label Feb 11, 2021
@cderv cderv self-assigned this Feb 11, 2021
@cderv
Copy link
Collaborator Author

cderv commented Feb 11, 2021

The knitted document contains some HTML code for the figure

<img src="test_files/figure-docx/unnamed-chunk-1-1.png" title="alt text" alt="alt text"  />

We don't want that for word_output.

Ideally we would like to generate the markdown syntax that work for docx #1900 (comment)

@cderv
Copy link
Collaborator Author

cderv commented Feb 11, 2021

Instead of juts fixing this, I would like to try understand better the plot logic.
When do we generate Pandoc Markdown, regular markdown or directly html or latex.

Currently, the plot logic is this:

  • hook_plot_md will do the dispatch
  • If not using Pandoc or is_html_output() is TRUE, use hook_plot_md_base()
  • If fig.show == 'animate' or is_tikz_dev(options)) && is_latex_output() than use hook_plot_tex()
  • If some option are used (c('out.width', 'out.height', 'out.extra', 'fig.align', 'fig.subcap','fig.env', 'fig.scap')), and
    • The output is latex: use hook_plot_tex()
    • The output is office document : use hook_plot_md_pandoc()
  • in all other case, use hook_plot_md_base()

After that, hook_plot_md_base() as also a logic where it will output 2 possible syntax

  • markdown : if some options are unset
  • HTML: to include raw if some options are set that would prevent to use markdown syntax.

This means that the output that will be written in the markdown file by knitr will be very dependent of set of options and formats - it will not be the same for each format. Maybe we could review that for later to something simpler like

  • If Pandoc is used, use pandoc syntax unless we want to support something that Pandoc does not.
  • Check Pandoc version as newer version supports more feature for Images.

For now, we just need to insure that docx output are recognized and markdown is used.

@cderv
Copy link
Collaborator Author

cderv commented Feb 11, 2021

BTW, this is how Pandoc 2.11.4 is parsing the Image syntax:

❯ echo '![la lune](lalune.jpg "Voyage to the moon")' | pandoc -t native
[Para [Image ("",[],[]) [Str "la",Space,Str "lune"] ("lalune.jpg","fig:Voyage to the moon")]]
❯ echo '![la lune](lalune.jpg "Voyage to the moon")' | pandoc -t json | jq .
{
  "pandoc-api-version": [
    1,
    22
  ],
  "meta": {},
  "blocks": [
    {
      "t": "Para",
      "c": [
        {
          "t": "Image",
          "c": [
            [
              "",
              [],
              []
            ],
            [
              {
                "t": "Str",
                "c": "la"
              },
              {
                "t": "Space"
              },
              {
                "t": "Str",
                "c": "lune"
              }
            ],
            [
              "lalune.jpg",
              "fig:Voyage to the moon"
            ]
          ]
        }
      ]
    }
  ]
}

And what it is translated to

❯ echo '![la lune](lalune.jpg "Voyage to the moon")' | pandoc -t latex
\begin{figure}
\centering
\includegraphics{lalune.jpg}
\caption{la lune}
\end{figure}

❯ echo '![la lune](lalune.jpg "Voyage to the moon")' | pandoc -t html5
<figure>
<img src="lalune.jpg" title="Voyage to the moon" alt="la lune" /><figcaption aria-hidden="true">la lune</figcaption>
</figure>

❯ echo '![la lune](lalune.jpg "Voyage to the moon")' | pandoc -t html4
<div class="figure">
<img src="lalune.jpg" title="Voyage to the moon" alt="" />
<p class="caption">la lune</p>
</div>

For DOCX, this in markdown

```r
plot(mtcars)
```

![A plot](test_files/figure-docx/unnamed-chunk-1-1.png "Description")

Will lead to having

  • "A plot" as a image caption/legend
  • Both "A plot" and "Description" as alternative text description in WORD I got
Description

A plot

I post this here for later reference because I looked into it.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bugs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant