2

I have numbers I need to put into my text in latex. The only possibility I know is using \input. So I put every single number in its own file. E.g. the file foo.dat contains 6.062843465325870040e-01 (I checked for trailing white spaces).

The first problem that appears is, that latex apparently puts a space after \input. So My number is \input{foo.dat}. yields

My number is 6.062843465325870040e-01 .

Note the unwanted space before the dot ..


The next problem is, that I obviously want properly formatted numbers. So I use siunits' \num. But \num{\input{foo.dat}} yields an error.

How can I fix this, or are there better solutions (without generating the properly formatted latex code as a file).


I considered https://stackoverflow.com/questions/29078107/insert-values-from-a-file-in-a-latex-document for my solution so far.

  • 1
    do you really need to just have one number per file? – David Carlisle Mar 3 '18 at 1:05
  • No, preferable I would like to save a dictionary with key - number pairs. Saving one number per file is just a workaround to use Latex as I know it. (I am aware of solutions like using Python from within Latex. At the moment I don't have time to investigate these things, so I prefer plain Latex.) – DerWeh Mar 4 '18 at 15:40
4

\unskip is your friend.

% arara: pdflatex
\RequirePackage{filecontents}
\begin{filecontents*}{foo.dat}
  6.062843465325870040e-01
\end{filecontents*}

\documentclass{article}

\begin{document}
My number is
  \textit{\input{foo.dat}\unskip}.
\end{document}

For usage with siunitx you might want to use the catchfile package:

% arara: pdflatex
\begin{filecontents*}{foo.dat}
  6.062843465325870040e-01
\end{filecontents*}

\documentclass{article}
\usepackage{siunitx}
\usepackage{catchfile}

\begin{document}
My number is
  \CatchFileDef{\foonum}{foo.dat}{}%
  \num\foonum.
\end{document}

output

If you have to do this very often it might be useful to wrap this process into a macro.

% arara: pdflatex
\begin{filecontents*}{foo.dat}
  6.062843465325870040e-01
\end{filecontents*}

\documentclass{article}
\usepackage{siunitx}
\usepackage{catchfile}

\newcommand*\OutputFileNum[2][\num]{%
  \CatchFileDef{\tempnum}{#2}{}%
  #1{\tempnum}%
}

\begin{document}
My number is \OutputFileNum{foo.dat}.
\end{document}
  • Thanks this answer works. I am a bit annoyed that I need to save the number to a variable first and then use the variable. I find this very ugly as I use the number only once. But I as long as I can hide it behind an command it is at least a very convenient workaround. – DerWeh Mar 3 '18 at 8:10
  • @egreg What a stupendous oversight of mine... Thanks! – Ruben Mar 3 '18 at 10:16
  • @DerWeh a slight caveat of the input implementation, but(!) with the variable approach the spacing problem disappears ;) – Ruben Mar 3 '18 at 10:29
2

You wrote:

I have numbers I need to put into my text in latex. The only possibility I know is using \input. So I put every single number in its own file.

Another solution would consist of defining macros, named (say) \numA, \numB, etc, as follows

\newcommand\numA{6.062843465325870040e-01}
\newcommand\numB{3.141592653589793238e-00}

in the preamble and to use them in the body of the text as follows:

My first number is \numA.

If you want to apply some formatting, e.g., employ "thousands separators", load the siunitx package and write

My second number is \num{\numB}.

A full MWE (minimum working example):

enter image description here

\documentclass{article}
\usepackage{siunitx} % for '\num' macro
\newcommand\numA{6.062843465325870040e-01}
\newcommand\numB{3.141592653589793238}
\begin{document}
My first number is \numA.

My second number is \num{\numB}.
\end{document}
  • While this indeed is a solution, it doesn't match my requirements. The numbers in the files will regularly change, so I can't hard-code them in my latex code. (Unless I let my code producing the numbers also change my Latex files. But I strictly refuse this workflow!) So the solution would be to actually put the latex markup \newcommand.... into the files. Again I would just consider it as an ugly hack. – DerWeh Mar 3 '18 at 8:13
0

A easy way to store numbers (as many as you want in a single file) and retrieve single numbers can be done with LaTeX + R integration via knitr. Better, you can do any math with these numbers in the same document. If you do not know what on earth I'm talking about, just install Rstudio and R, save the both files below in the same testing directory, open test.Rnw with Rstudio and click on the "Compile PDF".

test.dat

3.037494573458934585e-04
6.062843465325870040e-01
9.331454567567567675e-05

test.Rnw

\documentclass{article}
\begin{document}
<<loaddata,echo=F,comment="">>=
mynumbers <-read.csv(file="test.dat",header=F)
x <- formatC(mynumbers$V1[2], format = "e", digits = 18)
@
My first  number is \Sexpr{mynumbers$V1[1]}.\par
My second number is \Sexpr{mynumbers$V1[2]}.\par 
My third  number is \Sexpr{mynumbers$V1[3]}.\par 
\bigskip
<<changeoptions,echo=F>>=
options(scipen=2,digits=19)
@
My second number roughly is \Sexpr{round(mynumbers$V1[2],2)}, 
more precisely  \Sexpr{round(mynumbers$V1[2],5)}.\par
My second number is \Sexpr{mynumbers$V1[2]}.\par
My second number is \Sexpr{x}.\par 
\end{document}

Result:

MWE

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.