4

A value is saved in a file (value.txt, let's say). The command

\SI{\input{value.txt}}{\someunit}

gives the error:

Argument of \input has an extra }

How can I read numbers from an external file and use them with \SI?

Foo Bar
  • 13,247
dPol
  • 253
  • 2
    Welcome to TeX.sx! Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. – Peter Jansson Apr 11 '13 at 08:36
  • If you are reading data from a table, then \input is not the correct approach: look at datatool or pgfplotstable. – Joseph Wright Apr 11 '13 at 08:42
  • 1
    @dPol, Instead of including the answer in your post, you should accept a given answer instead. – Foo Bar Apr 11 '13 at 12:40
  • @FooBar thanks for your answer, it inspired my "answer", of course. I thought that my change was quite deep and therefore it deserved to be posted as a separete answer. Unfortunately I could not answer to my own question. – dPol Apr 11 '13 at 13:06
  • Comments are not for discussion of these kind of things, but this website works in a question-answer manner. Even "inspiring" answers are answers and should not be mixed with the questions. Currently your question is no question but a "statement". If it'S OK for you I can edit my answer with your adapted code. – Foo Bar Apr 11 '13 at 13:15
  • no problem. No copyright on it ;) – dPol Apr 11 '13 at 13:25

1 Answers1

6

You have to read the file with TeX commands:

\documentclass{article}
\usepackage{siunitx}

\def\inputval{0}
\newread\inputFile
\let\oldSI\SI

\renewcommand*{\SI}[3][]{%
  \IfFileExists{#2}{
    \openin\inputFile=#2
    \read\inputFile to \inputval
    \closein\inputFile
    \oldSI[#1]{\inputval}{#3}
  }{\oldSI[#1]{#2}{#3}}
}

\begin{document}
  \SI{value.txt}{\metre} % use \SI like always, but with input file
  \oldSI{5}{\metre}      % \oldSI does work like the old \SI ;)
\end{document}

Of course, this could be heavily improved (check if the format is correct, etc.), but that's the way to go.

Foo Bar
  • 13,247