2

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?

  • 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 8: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 8: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
4

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.

|improve this answer|||||
  • I include tens of values in a single document. Is it possible to avoid 3 lines and 2 new variables definition? – dPol Apr 11 '13 at 8:40
  • You can make the file reading code a command, like \newcommand*{\readNumber} [1] {here the code}, where the only argument is the file to be read. – Foo Bar Apr 11 '13 at 8:42
  • 2
    @FooBar I've moved the \newread line outside the definition, where it's wrong. – egreg Apr 11 '13 at 9:24
  • 1
    These are big questions on their own and should not be discussed in the comments (and I'm sure the answers can be found on this site already). – Foo Bar Apr 11 '13 at 10:15
  • 1

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.