# How to import a sentence from a .txt file that will preserve the font characteristics of the rest of the paragraph?

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.

• 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 '21 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. Sep 30 '21 at 1:42

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}
\begin{document}
We found the predicted rise in temperature to be
$\mydata^{\circ}$C.
\end{document}


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}
\begin{document}
We found the predicted rise in temperature to be
$\mydataA^{\circ}$C.

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

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

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


• 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 \mydata1 is invalid name for a macro, which can only contain "letters". (catcode-11 tokens). Try the same with \mydataA and \mydataB Sep 30 '21 at 15:16