2

In my implementation of an automatic word count (adapted from this solution), only the count and nothing else is output to a text file, to be inserted into the tex document with \input. This works fine, but not when combined with siunitx's \num for number formatting, which produces an error. The problem is likely the newline character that \input automatically added. My attempt to circumvent the newline with this solution doesn't help. How can I make this work?

\documentclass{article}
\usepackage[group-minimum-digits=4,group-separator={,}]{siunitx}

\begin{filecontents*}{\jobname_wordcount.txt}
1234
\end{filecontents*}
  
\newcommand\minput[1]{% doesn't help
  \input{#1}%
  \ifhmode\ifnum\lastnodetype=11 \unskip\fi\fi}

\begin{document}

\input{\jobname_wordcount.txt}words % works
% \num\input{\jobname_wordcount.txt}words % causes error

\end{document}
2
  • Another expansion problem (\input is not expandable). Usually there would be a functional answer, but it doesn't seem to support optional argument yet.
    – user202729
    Jun 21, 2022 at 0:32
  • @user202729 Optional argument is in my todo list. I will add an answer after optional argument is supported.
    – L.J.R.
    Jun 21, 2022 at 4:20

2 Answers 2

1

You can instead load the file's content in a token list and use that as the argument to \num.

\documentclass{article}
\usepackage[group-minimum-digits=4,group-separator={,}]{siunitx}

\begin{filecontents*}{\jobname_wordcount.txt}
1234
\end{filecontents*}

\ExplSyntaxOn
\NewDocumentCommand{\numinput}{O{}m}
 {
  \file_get:nnN { #2 } { } \l_tmpa_tl
  \num[#1]{\l_tmpa_tl}
 }
\ExplSyntaxOff

\begin{document}

\numinput{\jobname_wordcount.txt} words

\numinput[group-separator={\,}]{\jobname_wordcount.txt} words

\end{document}

enter image description here

Without expl3 you can use catchfile to the same purpose.

\documentclass{article}
\usepackage[group-minimum-digits=4,group-separator={,}]{siunitx}
\usepackage{catchfile}

\begin{filecontents*}{\jobname_wordcount.txt}
1234
\end{filecontents*}

\makeatletter
\newcommand{\numinput}[2][]{%
  \CatchFileDef{\numinput@text}{#2}{}%
  \num[#1]{\numinput@text}%
}
\makeatother

\begin{document}

\numinput{\jobname_wordcount.txt} words

\numinput[group-separator={\,}]{\jobname_wordcount.txt} words

\end{document}
5
  • Did you make a mistake when pasting the first chunk of code? It seems to me that the two solutions are the same, both using the catchfile package. There's nothing on expl3 beside your mention of it.
    – bongbang
    May 22, 2022 at 4:26
  • @bongbang Fixed.
    – egreg
    Jun 21, 2022 at 9:11
  • I think the syntax of the l3file package has changed and the relevant command should be \ior_str_get:NN. Jun 16, 2023 at 16:13
  • @JasperHabicht No, that's for saving the input in the form of a str. Not much difference, in the particular case, though.
    – egreg
    Jun 16, 2023 at 16:17
  • Ah, you are right. It still works. Sorry, my bad! The error I got was due to something else obviously. Jun 16, 2023 at 16:19
1

Some day in the future (might be near) the functional package will support optional argument and wrapper for file_get, and you can use that.

For now, you can workaround by defining yet another wrapper layer.

%! TEX program = lualatex
\documentclass{article}
\usepackage{functional}
\usepackage[group-minimum-digits=4,group-separator={,}]{siunitx}

\begin{filecontents*}{\jobname_wordcount.txt}
1234
\end{filecontents*}

\ExplSyntaxOn

\PrgNewFunction \fileGet {m} {
 \file_get:nnN { #1 } { } \gResultTl
}

\PrgNewFunction \numWrapperInner {mm} {
 \num[#1]{#2}
}

\NewDocumentCommand{\numWrapper}{O{}m}
{
 \numWrapperInner{#1}{#2}
}

\ExplSyntaxOff

\begin{document}

\numWrapper{1234} words  % use normally

\numWrapper{\fileGet{\jobname_wordcount.txt}} words  % use with fileGet function

\numWrapper[group-separator={\,}]{\fileGet{\jobname_wordcount.txt}} words  % and optional argument

\end{document}

(remark, the same principle can be used for other argument types/argument preprocessor etc. as well.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .