4

I want to center image in R markdown in knit word.

I search result almost is CSSor HTML code.

But I need center image in word.

This my code

![avatar](C:/Users/user/Desktop/picture1.jpg){ width=5cm }

Thanks!

disney82231
  • 189
  • 1
  • 3
  • 11
  • 1
    Possible duplicate of [Centering image in RMarkdown for Word export](https://stackoverflow.com/questions/42228002/centering-image-in-rmarkdown-for-word-export) – emilliman5 Apr 04 '18 at 18:22
  • sorry, I did not understand what his means. How can set this macro in my markdown? – disney82231 Apr 04 '18 at 18:28
  • I think that means that it cannot be done easily. The OP (Ben) self-answered saying they reverted to using VBA to remedy the problem. I agree with Ben that this solution is *"not fully satisfactory"* ... in fact, I'd argue it indicates a big hole in the process. But to answer your question: *"How can set this macro in my markdown?"*, I'm afraid there may be no way to do it there. – r2evans Apr 04 '18 at 19:21

1 Answers1

5

I encountered the same problem and managed to solve it using a .docx file template that I declared at the beginning of the document:

---
output:
    word_document:
        reference_docx: template.docx
---

Just open your template file, select a figure, then press Ctrl + Shift + s to display the style panel for Microsoft Word. Change the alignment style of the text to "centered". Save your template.docx then knit your document as usual.

godot
  • 1,550
  • 16
  • 33
Annesohie
  • 51
  • 1
  • 2
  • 1
    For future people wondering which style of text is changed: in the style panel there are "Figure" and "Figure with Caption" styles. – Nuclear03020704 Mar 15 '20 at 14:12
  • I used this but my Rmd has children and the children threw an error. This worked to fix it. Note I had to use `!expr` to get it to work. `reference_docx: !expr file.path(here::here(), "template.docx")` – Eli Holmes Jun 15 '21 at 21:53
  • Also I originally called my template `template.dotx` because that is what Word saved it as. But **flextable** then wouldn't run (complained that it could only use `.docx` ref files). Renamed file it to `template.docx` and it worked ok. – Eli Holmes Jun 15 '21 at 21:54