I am trying to add a subscript to a caption generated in officer. I am creating the caption like this:
library(officer)
doc <- read_docx('empty_file.docx')
autonum <- run_autonum(seq_id = 'fig', pre_label = 'Figure ')
caption <- block_caption(label='My caption includes SO2.', style = "caption", autonum = autonum)
doc <- body_add_caption(doc, caption)
print(doc, target = 'output.docx'))
However, now I'd like to put the '2' in 'SO2' in subscript. I know how to generate subscripts:
fp_text_prop <- fp_text(color='orange')
prop_mod <- update(fp_text_prop, vertical.align = 'subscript')
paragraph <- fpar(ftext('SO', prop = fp_text_prop), ftext('2', prop = prop_mod)))
But I can't use the resulting fpar
inside a caption, as body_add_caption
expects the output from block_caption
and block_caption
expects a normal string as argument for the label=
.
How can I put an fpar
, or alternatively, a subscript inside a caption?
I have found a solution that is a bit involved but seems to work.
library(officer)
doc <- read_docx('empty_file.docx')
autonum <- run_autonum(seq_id = 'fig', pre_label = 'Figure ')
fp_text_prop <- fp_text(color='orange')
prop_mod <- update(fp_text_prop, vertical.align = 'subscript')
caption <- fpar(autonum, ftext('SO', prop = fp_text_prop), ftext('2', prop = prop_mod)))
doc <- body_add_fpar(x=doc, value=caption, style = 'caption')
print(doc, target = 'output.docx'))
There are a couple of caveats: fp_text_prop
should match the normal style of the captions and style = 'caption'
should be changed to pick the correct style for captions in your document.