5

I would like to write small tex files containing values with an external program and then show the result in my main tex file using \input in an siunitx S column.

Lets assume a file named pi_file.tex with the content 3.141592654 exists in the current folder.

The minimal (non-working) example is as follows:

\documentclass{article}
\usepackage{siunitx}
\usepackage{xcolor}

\begin{document}
\begin{table} \centering
    \caption{Table with \texttt{S} column type.}
    \begin{tabular}{cS}
        \hline
        Symbol & {Value} \\
        \hline
        $\pi$ & \input{pi_file.tex} \\
        Red $\pi$ & \color{red} 6.283185307 \\
        \hline
    \end{tabular}
\end{table}
\end{document}

I think, the macro expansion for \input takes place later than the work needed for siunitx S column formatting. How can I still use my external file pi_file.tex with siunitx S columns?

The result should of course look like this: example output

Based on a comment, I managed to load a value in the table, but I am not able to make it a new command which takes the filename as an argument:

\documentclass{article}
\usepackage{siunitx}
\usepackage{xcolor}

\def\inputval{0}
\newread\inputFile
\openin\inputFile=pi_file.tex
\read\inputFile to \inputval
\closein\inputFile

\begin{document}
\begin{table} \centering
    \caption{Table with \texttt{S} column type.}
    \begin{tabular}{cS}
        \hline
        Symbol & {Value} \\
        \hline
        $\pi$ & \inputval \\
        Red $\pi$ & \color{red} 283186.12 \\
        \hline
    \end{tabular}
\end{table}
\end{document}
  • maybe tex.stackexchange.com/questions/108087/… can help – user36296 Oct 24 '17 at 14:55
  • @samcarter: As soon as I pit it in a \def it works without siunitx S columns but not with the S columns anymore. The example I gave in the edit of course is not very practical. – Lukas Oct 24 '17 at 16:41
6
+100

You need an expandable version of \input; the standard one in LaTeX isn't, the TeX primitive \@@input is.

Here I use filecontents just to make the example self-contained (and to not clobber my files).

\begin{filecontents*}{\jobname-pi.tex}
3.141592654
\end{filecontents*}

\documentclass{article}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{booktabs}

\makeatletter
\newcommand{\expinput}[1]{\@@input #1}
\makeatother

\begin{document}

\begin{table}
\centering
\caption{Table with \texttt{S} column type.}

\begin{tabular}{cS[table-format=1.9]}
\toprule
Symbol & {Value} \\
\midrule
$\pi$ & \expinput{\jobname-pi.tex} \\
Red $\pi$ & \color{red} 6.283185307 \\
\bottomrule
\end{tabular}

\end{table}

\end{document}

enter image description here

  • +1 (I learned this primitive command and I will sleep happier :) )... A suggestion is to change the numbers of your example so that S column type's properties will be shown on your picture... – koleygr Oct 29 '17 at 22:41
4

like this?

enter image description here

i'm not sure if i correctly understood your question. anyway, above table is generated by:

\documentclass[margin=3mm,preview]{standalone}
\usepackage{siunitx}
\usepackage{xcolor}

\def\inputval{0}
\newread\inputFile
\openin\inputFile=pi_file.tex
\read\inputFile to \inputval
\closein\inputFile

\usepackage{etoolbox}% <-- new
\newcommand{\sred}{\color{red}} % renew def. for colored font
\robustify\sred

\begin{document}
    \begin{table}
\centering
    \caption{Table with \texttt{S} column type.}
\begin{tabular}{cS[table-format=1.9]}
        \hline
Symbol          & {Value} \\
        \hline
$\pi$           & \inputval \\
Red $\pi$       & \sred 3.141 592 653 \\
        \hline
\end{tabular}
    \end{table}
\end{document}
  • I appreciate the answer, but it does not expand well to more than one input file. I see, that it relates to my second code example (which was the best I could do without foreign help so far). – Lukas Oct 30 '17 at 21:47
2

This may helps to do what you want:

\documentclass{article}
\usepackage{siunitx}
\usepackage{xcolor}
\usepackage{pgffor}
\usepackage{filecontents}

\begin{filecontents*}{InpFile1.tex}
3.1   
\end{filecontents*}
\begin{filecontents*}{InpFile2.tex}
3.14   
\end{filecontents*}
\begin{filecontents*}{InpFile3.tex}
13.1415   
\end{filecontents*}

\newcounter{myInputCounter}

\newcommand{\myinputcomand}[2]{
\def\inputval{0}
\newread\inputFile
\openin\inputFile=#1\themyInputCounter.tex
\read\inputFile to \inval
\closein\inputFile
\global\expandafter\let\csname #2\themyInputCounter\endcsname\inval
}

%Arguments for ReadNEnumInputFromEnumFiles command:
%1. The number of files (and values) that will be used
%2. The basic name of the files (without the following enumeration and the `.tex` extension)
%3. The basic name of the values (without their enumeration).
\newcommand{\ReadNEnumInputFromEnumFiles}[3]{
\setcounter{myInputCounter}{0}
\foreach \i in {1,...,#1}{\stepcounter{myInputCounter}\myinputcomand{#2}{#3}}
}

\begin{document}
\ReadNEnumInputFromEnumFiles{3}{InpFile}{InVal}
\begin{table} \centering
    \caption{Table with \texttt{S} column type.}
    \begin{tabular}{cS}
        \hline
        Symbol & {Value} \\
        \hline
        Red $\pi$ & \color{red} 13.12 \\
        next enumerated &\\
        no 1 & \csname InVal1\endcsname\\
        no 2 & \csname InVal2\endcsname\\
        no 3 & \csname InVal3\endcsname\\
        \hline
    \end{tabular}
\end{table}
\end{document}

You have to give the command \ReadNEnumInputFromEnumFiles before the table.

with arguments:

  1. The number of files (and values) that will be used
  2. The basic name of the files (without the following enumeration and the .tex extension)
  3. The basic name of the values (without their enumeration).

Then in the table you have to use the values inside \csname (As you named them in the second argument of previous command and with their number in the end)

Output:

enter image description here

  • Nice idea with the enumeration. Nevertheless, it added to much complexity to be the reference answer. – Lukas Oct 30 '17 at 21:48
  • Nice idea with the enumeration. Nevertheless, it added to much complexity to be the reference answer. – Lukas Oct 30 '17 at 21:49
  • No problem @Lukas. I would select the same answer too... But you have to look the user interface and not the complexity of the commands used... (I just told that to you because I am one that has to hear it too...) If you think about the commands inside a usepackage you can not go on... My answer adds just some pluralism and may be help some people or solve similar problems.. or give an idea etc... This is the reason of its existence. – koleygr Oct 30 '17 at 22:00

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.