expl3 has defeated me again so I'm relying on the kindness of strangers again.
I think the underlying problem is more about splitting input rather than the file reading.
Context:
I want to load the parameters to the geometry package's \geometry{} command from a file using expl3. I have tried at least 4 ways of reading the file. But however I load the file, the entire contents is being passed to \geometry{} as a single token / string / block / thing because the geometry package complains the entire thing top=2cm,bottom=2cm is not a valid key Package keyval Error: top=2cm,bottom=2cm undefined. Yet if I put \geometry{top=2cm,bottom=2cm} in the document then that is processed correctly.
I have seen this answer which splits key-value pairs but I need all the key-value pairs at once in the input stream not one at a time.
Minimal working examples
This works:
\documentclass{article}
\ExplSyntaxOn % Also works without expl3 syntax
\usepackage{geometry}
\geometry{top=2cm,bottom=2cm}
\ExplSyntaxOff
\begin{document}
Document content.
\end{document}
Minimal not working examples
(Not using the recommended expl3 naming conventions for brevity)
Token list
Token list fails resulting in Package keyval Error: top=2cm,bottom=2cm undefined.
\documentclass{article}
\ExplSyntaxOn
\tl_clear_new:N \geotoks_tl
\tl_set:Nn \geotoks_tl {top=2cm,bottom=2cm}
% Also fails with {top=2cm, bottom=2cm}
\usepackage{geometry}
\geometry{\geotoks_tl} % fail
% \geometry{\tl_use:N \geotoks_tl} % fail
\ExplSyntaxOff
\begin{document}
Document content.
\end{document}
String
String fails resulting in Package keyval Error: top=2cm,bottom=2cm undefined.
\documentclass{article}
\ExplSyntaxOn
\str_clear_new:N \geotoks_str
\str_set:Nn \geotoks_str {top=2cm,bottom=2cm}
% Also fails with {top=2cm, bottom=2cm}
\usepackage{geometry}
\geometry{\geotoks_str} % fail
% \geometry{\str_use:N \geotoks_str} % fail
\ExplSyntaxOff
\begin{document}
Document content.
\end{document}
Clist
Clist fails resulting in Package keyval Error: top=2cm,bottom=2cm undefined.
\documentclass{article}
\usepackage{geometry}
\ExplSyntaxOn
\clist_clear_new:N \geotoks_clist
\clist_set:Nn \geotoks_clist {top=2cm, bottom=2cm}
\usepackage{geometry}
\geometry{\clist_use:Nn \geotoks_clist {,}}
% Also fails with
% \geometry{\clist_use:Nn \geotoks_clist {~}}
% though error message is Package keyval Error: top=2cm bottom=2cm undefined.
\ExplSyntaxOff
\begin{document}
Document content.
\end{document}
Seq
\documentclass{article}
\usepackage{geometry}
\ExplSyntaxOn
\clist_clear_new:N \geotoks_clist
\clist_set:Nn \geotoks_clist {top=2cm, bottom=2cm}
\seq_clear_new:N \geotoks_seq
\seq_set_from_clist:NN \geotoks_seq \geotoks_clist
\usepackage{geometry}
\geometry{\seq_use:Nn \geotoks_seq {,}}
% Also fails with {~} as the separator
\ExplSyntaxOff
\begin{document}
Document content.
\end{document}
I have also met the same fail with splitting into a sequence as described here.
File loading
I have met the same Package keyval Error: top=2cm,bottom=2cm undefined. fails by using the file loading methods in these answers:
In desperation I also tried \geometry{\file_input:n {geometryParams.txt}} which at least gave me a different error Missing \endcsname inserted. Same error with \geometry{\input{params.txt}} inside or outside ExplSyntax.
\ExpandArgs{o}\geometry{\geotoks_str}– Ulrike Fischer Sep 05 '23 at 12:04\ExpandArgsbefore. – Doc Octal Sep 05 '23 at 12:32