This function is an easy way to add a figure to your document if the figure is already available in a png file. cat() is used to print the information to the screen; so, when your .Rmd file is rendered, the resulting LaTeX file will include all the information that was printed to the screen.

add_figure(
  filein,
  caption = "Add figure caption",
  alt_caption = "",
  label,
  width = 100,
  height = 100
)

Arguments

filein

The path of the figure to be added (e.g., "C:\\My figure directory\\plot.png"). Relative paths are not recommended because the path of your working directory will change when you are rendering to the working directory of the file that you are rendering and the relative path no longer be valid.

caption

A character string providing the figure caption that will be added below the figure in the document. A default text string is provided but it is not informative and should be changed. Consider being more verbose here than typical and remember that captions should be able to stand on their own to ensure their portability between media. Commas are not allowed in the alternative text and all LaTeX should be double escaped (e.g., "\\%, "\\$R\\_0\\$").

alt_caption

A character string providing alternative text for the figure. The default is "", which will force the alternative text to be blank. Using NULL will force the alternative text to also be blank; previously, this option copied the caption to the alternative text, which leads to the screen reader reading the same text twice. Note, that the default is not ideal. Instead, alternative text that describes the take-home message or information that is not available in the caption should be included. Commas are not allowed in the alternative text and all LaTeX should be double escaped (e.g., "\\%, "\\$R\\_0\\$").

label

A character string that will be used as the figure reference for citation of figure in the document. There is no default value for this argument because each figure needs to have a unique label that is known by the user, and thus, the user needs to specify it.

width, height

A numeric value between 0 and 100 that dictates the figure width or height in terms of a percentage of the or in the document. The default is 100. height`` does not work in html mode because there is no concept of page size; instead, use width` to scale the figure up or down.

Value

A string is returned with the label of the figure. You can use this label to reference the figure elsewhere in the document.

cat() is used to print output to the screen if you run this function on its own or to a resulting rendered file if called within an .Rmd file, where the latter is more likely. Results are specific to the document being rendered, i.e., where knitr::is_html_output() is used to determine if your result is html or latex.

Details

Translation of code from markdown to tex is developing for figures as more and more features for accessibility are developed. This code offers a way to ensure that your figures are coded in the resulting .tex file such that they are most likely to take advantage of the latest and greatest features available. For example, when this function was written, alternative text via rmarkdown chunks was not an option. Instead, the insertion of a figure used html code. Now, for pdf files, there are tagging capabilities such that screen readers can access the alternative text without any post-processing of files if the .tex structure is set up properly. Thus, rather than constantly having to change your document ... just rely on the code within in this function to ensure that your .tex files are up to date. Happy texting!

For potentially more information see the GitHub Discussion Board and search for accessibility.

Author

Chantel R. Wetzel

Examples