1

I have numbers from an R script that I would like to include in a report that I am typing in LaTeX. There are enough numbers that I am concerned about making a typo, and I don't want to do copy/paste over and over, either.

Doing something over and over? That sounds like a job for a computer!

I have found it easy to save graphs and include them in LaTeX files, and I figure I should be able to do the same with a sentence. My idea is to have R print my sentence to a .txt file, and then I will call some command that will import the verbatim text. The sentence will include numbers that I will format in dollar signs to make them look math-y. An example sentence might be:

"We found the predicted rise in temperature to be $8.6 ^{\circ}C$."

I would use R to calculate and then print out the 8.6, which I know how to do.

I found this post that almost solves my issue, but I want the imported sentence to have the same font characteristics as the rest of the text in my paragraph, which the linked material does not solve.

2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Sep 30 at 0:29
  • The answer you linked to doesn't preserve the font, but the idea of the question to use \input would preserve the font. If you \input{somefile}, it will behave as if the contents of somefile were pasted at that location. Which is more viable if you are the one controlling the R output.
    – Teepeemm
    Sep 30 at 1:42
3

The readarray package has \readdef, which can read a file's contents into a \def.

\begin{filecontents*}[overwrite]{mydata.txt}
8.6
\end{filecontents*}
\documentclass{article}
\usepackage{readarray}
\readarraysepchar{}
\begin{document}
\readdef{mydata.txt}\mydata
We found the predicted rise in temperature to be 
$\mydata^{\circ}$C.
\end{document}

enter image description here


SUPPLEMENT

Here, I show the OP how to handle either multiple input files or multiple data in a single input file. If only a single datum is given per line, \readrecordarray can be used. Otherwise, fully 2-D (or even 3-D) array data may be read from the file with \readarray, knowing the appropriate field separators.

Compiled with the 2021-09-17 version of readarray, found at https://ctan.org/tex-archive/macros/latex/contrib/readarray

\begin{filecontents*}[overwrite]{mydata1.txt}
8.6
\end{filecontents*}
\begin{filecontents*}[overwrite]{mydata2.txt}
3.5
\end{filecontents*}
\begin{filecontents*}[overwrite]{mydata3.txt}
8.6, 10.3
3.5, 5.4
\end{filecontents*}
\begin{filecontents*}[overwrite]{mydata4.txt}
8.6
3.5
\end{filecontents*}
\documentclass{article}
\usepackage{readarray}[2021-09-17]
\begin{document}
\readdef{mydata1.txt}\mydataA
We found the predicted rise in temperature to be 
$\mydataA^{\circ}$C.

\readdef{mydata2.txt}\mydataB
We found the predicted rise in temperature to be 
$\mydataB^{\circ}$C.

\readarraysepchar{,}
\readdef{mydata3.txt}\mydataC
\readarray*\mydataC\arrayC[-,2]
Test 1 ranged from $\arrayC[1,1]$--$\arrayC[1,2]^{\circ}$C.

Test 2 ranged from $\arrayC[2,1]$--$\arrayC[2,2]^{\circ}$C.

\readrecordarray{mydata4.txt}\arrayD
Tests ranged from \arrayD[1] to \arrayD[2].
\end{document}

enter image description here

3
  • I have gotten this to work to some extent, but it is breaking down when I want to import multiple numbers, which I am doing with one number per file: something like \readdef{mydata1.txt}\mydata1 and \readdef{mydata2.txt}\mydata2 and then $\mydata1^{\circ}$C and $\mydata2^{\circ}$C. Thoughts? (I'm not determined to have one number per .txt file, but that seemed like the easiest way to use this answer.)
    – Dave
    Sep 30 at 11:32
  • 1
    @Dave \mydata1 is invalid name for a macro, which can only contain "letters". (catcode-11 tokens). Try the same with \mydataA and \mydataB Sep 30 at 15:16
  • 1
    @Dave Please see the supplement to my answer. Sep 30 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.