rMarkdown: compile word docx document with line numbers - ms-word

I am trying to produce a document with rMarkdown code that will render a word docx document that contains continuous line numbers. This is to facilitate workflow for publication submissions where line numbers are often requested.
I have tried using a style docx document with line numbers that was originally rendered from rMarkdown and subsequently edited to change heading style and line numbers. Heading style does change according to that style document but line numbers are not introduced. Desperate latex package use also didn't work in yaml as below.
---
output:
pdf_document:
citation_package: natbib
keep_tex: yes
fig_caption: yes
latex_engine: pdflatex
word_document:
reference_docx: ele_style.docx
geometry: margin=1in
fontfamily: mathpazo
fontsize: 11pt
mainfont: Times New Roman
linespread: 2
header-includes:
-\usepackage{helvet}
-\usepackage{setspace}
-\doublespacing
-\usepackage{lineno}
-\linenumbers
bibliography: LitRev_Bibli.bib
biblio-style: Zotero/styles/ecology-letters.csl
---
Does anyone know how to render line numbers into word docx from rMarkdown?

Related

How do I cross-reference objects by page number in R Markdown to Microsoft Word?

I am wondering how to cross-reference by page number, similar to the way that you can cross-reference section numbers (https://bookdown.org/yihui/bookdown/cross-references.html) when generating Word documents from .Rmd files.
I am looking for something like the pageref command from LaTeX ([https://en.wikibooks.org/wiki/LaTeX/Labels_and_Cross-referencing#Introduction])1, but for R Markdown to Word.

pandoc markdown to docx - keep list on one page

I have a markdown list like so:
* Question A
- Answer 1
- Answer 2
- Answer 3
I need to ensure that all the answers (1 - 3) appear on the same page as Question A when I convert the markdown document to docx using pandoc. How can I do this?
Use custom styles in your Markdown and then define those styles in a custom docx template.
It's important to note that Pandoc's documentation states (emphasis added):
Because pandoc’s intermediate representation of a document is less
expressive than many of the formats it converts between, one should
not expect perfect conversions between every format and every other.
Pandoc attempts to preserve the structural elements of a document, but
not formatting details...
Of course, Markdown has no concept of "pages" or "page breaks," so that is not something Pandoc can handle by default. However, Pandoc is aware of docx styles. As the documentation explains:
By default, pandoc’s docx output applies a predefined set of styles
for blocks such as paragraphs and block quotes, and uses largely
default formatting (italics, bold) for inlines. This will work for
most purposes, especially alongside a reference.docx file. However, if
you need to apply your own styles to blocks, or match a preexisting
set of styles, pandoc allows you to define custom styles for blocks
and text using divs and spans, respectively.
If you define a div or span with the attribute custom-style, pandoc
will apply your specified style to the contained elements. So, for
example using the bracketed_spans syntax,
[Get out]{custom-style="Emphatically"}, he said.
would produce a docx file with “Get out” styled with character style
Emphatically. Similarly, using the fenced_divs syntax,
Dickinson starts the poem simply:
::: {custom-style="Poetry"}
| A Bird came down the Walk---
| He did not know I saw---
:::
would style the two contained lines with the Poetry paragraph style.
If the styles are not yet in your reference.docx, they will be defined
in the output file as inheriting from normal text. If they are already
defined, pandoc will not alter the definition.
If you don't want to define the style manually, but would like it applied to every list automatically (or perhaps to every list which follows a specific pattern), you could define a custom filter which applied the style(s) to every matching element in the document.
Of course, that only adds the style names to the output. You still need to define the styles (tell Word how to display elements assigned those styles). As the documentation for the --reference-doc option explains :
For best results, the reference docx should be a modified version of a
docx file produced using pandoc. The contents of the reference docx
are ignored, but its stylesheets and document properties (including
margins, page size, header, and footer) are used in the new docx. If
no reference docx is specified on the command line, pandoc will look
for a file reference.docx in the user data directory (see --data-dir).
If this is not found either, sensible defaults will be used.
To produce a custom reference.docx, first get a copy of the default
reference.docx: pandoc --print-default-data-file reference.docx >
custom-reference.docx. Then open custom-reference.docx in Word, modify
the styles as you wish, and save the file.
Of course, when modifying the custom-reference.docx in Word, you can add your new custom style which you have used in your Markdown. As #CindyMeister points out in a comment:
Word would handle this using styles, where the Question style would
have the paragraph setting "Keep with Next". the Answer style would
have this as well. A third style, for the last entry, would NOT have
the setting activated. In addition, all three styles would have the
paragraph setting "Keep together" activated.
Finally, when using pandoc to convert your Markdown to a Word docx file, use the option --reference-doc=custom-reference.docx and your custom style definitions will be included in the generated docx file. As long as you also properly identify which elements in the Markdown document get which styles, your should have a list which doesn't get broken across a page break as long at the entire list fits on one page.

MS Word: Carriage Returns in numbering format

in MS Word 2010, is it possible to include a manual line breaks in the formatting for a numbered list?
What I mean is I'm creating a style that includes numbering in a list. I'd like the list to appear like this:
Section 1[MLB]
Benefits
Section 2[MLB]
Drawbacks
etc.
I'm in the Define New Number Format dialog box, trying to find a way to include a manual line break in the Number Format field. I've got the word "Section" in there, but the line break is a problem so far. I've tried ^|, which is the search-and-replace code for manual line breaks. But that includes a literal carat followed by a pipe. Is there some other way of including things like paragraph breaks or line breaks in numbered lists? Thanks everyone.

RMarkdown with Knit to HTML: How to increase section number?

I am generating HTML pages with KnitR HTML from RMarkdown (using R Studio).
I need section numbering starting at an arbitrary number, not 0 or 1.
Example of what I am trying to achieve:
TITLE
2 SECTION
2.1 SUB-SECTION
I am using a code such as:
---
title: "TITLE"
output:
html_document:
number_sections: yes
---
# SECTION
## SUBSECTION
I found the following in this question.
The pandoc option --number-offset allows to specify the "offset for section headings in HTML output":
--number-offset=NUMBER[,NUMBER,…]
Offset for section headings in HTML output (ignored in other output formats). The first number is added to the section number for top-level headers, the second for second-level headers, and so on. So, for example, if you want the first top-level header in your document to be numbered “6”, specify --number-offset=5. If your document starts with a level-2 header which you want to be numbered “1.5”, specify --number-offset=1,4. Offsets are 0 by default. Implies --number-sections.
Quoted from the pandoc README file.

How to get rmarkdown and knitr to use em-dash with .docx files?

I am new to using rmarkdown and knitr to produce .docx word documents. The rmarkdown reference guide states that using -- gives an en-dash, and --- gives an em-dash.
If I knit my .Rmd file to HTML then the en-dashes and em-dashes are working correctly, however when knitting to a word document, they remain as -- and ---.
I am using Microsoft Word 2013, and I have checked the autoformatting options are set to replace hyphens with a dash.
Any help would be very much appreciated, thanks!
You need to add a pandoc_args: ["--smart"] to the header: Then it words for DocX format
---
output:
word_document:
pandoc_args: ["--smart"]
---