Combine siunitx S column with input value from tex file
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}
like this?
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}
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:
- The number of files (and values) that will be used
- The basic name of the files (without the following enumeration and the
.tex
extension) - 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: