I'm doing the Advent of Code (2022) in LaTeX3 (no, really I am). Each challenge involves some input, which for Day 6 consists of a single line which is very long (4096 characters). This line needs processing character by character, so I thought that I would get TeX to read it in character by character. Now, I know that TeX can cope with a token list of that length easily, but my purpose in doing the AoC in LaTeX3 is to better understand the language rather than to find the most efficient or effective solutions. So even though I ended up not using a character-by-character method, and read the whole file into a single token list, I'd still like to know how to do it.

Here's what I tried:

\cs_set_eq:Nc \aoc_input: {@@input} % to get an expandable file input
\cs_new:Npn \aoc_process_char:w #1
  \tl_if_eq:nnF {#1} {\par} % this doesn't work

\exp_after:wN \aoc_process_char:w \aoc_input: file.txt\relax

This doesn't work because the \par at the end of a file is \outer so can't be used in a parameter of a function. So reading around on this site, I saw the suggestion to \let something to each token in turn and test that, something like:

\expandafter\slurptoken\@@input file.txt\relax

I'm struggling to convert that to LaTeX3 syntax. It feels like one of the \peek_after:Nw type of functions should be what I'm looking for, but I need one that absorbs the token from the stream and the ones with remove in their name just test the charcode or catcode whereas I want to be able to do more processing with that token.

So what is the best LaTeX3 equivalent of the TeX code above?

1 Answer 1


The easiest, I think, is using

\peek_analysis_map_inline:n { <inline code> }

This function peeks ahead in the input stream, and feeds the tokens that follow to the <inline code>. The <inline code> receives three arguments:

  1. Something that o- or x-expands to the token seen;
  2. The character code of the token (-1 if it's a control sequence);
  3. The category code of the token (0 if it's a control sequence) in hex.

With that information you can likely do any processing you should need on the input.

\peek_analysis_map_inline:n is particularly nice because it goes to some length to differentiate a { from a \bgroup, which is not trivial (not possible with the usual \futurelet approach), and because it makes it very easy to process the token as you need.

Below is an example. It starts with the basics, by checking if the file exists. Then it does \tex_everyeof:D { \__loopspace_file_process_end: } to insert a token that signals the end of the file. Then it inputs the file, and processes it with \peek_analysis_map_inline:n.

\NewDocumentCommand \processfile { m }
  { \loopspace_file_process:n {#1} }
\cs_new_protected:Npn \loopspace_file_process:n #1
    \file_if_exist:nTF {#1}
      { \__loopspace_file_process:n {#1} }
      { \msg_error:nnn { loopspace } { file-not-found } {#1} }
\msg_new:nnn { loopspace } { file-not-found } { File~'#1'~not~found. }
\cs_new_protected:Npn \__loopspace_file_process:n #1
      \tex_everyeof:D { \__loopspace_file_process_end: }
      \exp_after:wN \__loopspace_file_process:w \tex_input:D {#1}
\cs_new_protected:Npn \__loopspace_file_process:w
        \int_compare:nNnT {##2} = { -1 }
            \exp_after:wN \token_if_eq_meaning:NNT
                ##1 \__loopspace_file_process_end:
              { \peek_analysis_map_break: }
        % process token:
        \exp_after:wN \token_to_str:N ##1 ~ ( ##2 ,~ ##3 ) \par
        % --------------
\cs_new_protected:Npn \__loopspace_file_process_end:
  { \msg_error:nn { loopspace } { premature-end } }
\msg_new:nnn { loopspace } { premature-break }

Lorem ipsum \textbf{dolor sit amet}. \bgroup{}$&#^_
\ttfamily \processfile{tmp.tex}
  • This looks great - I'm going to have to study it to understand it properly. Does \peek_analysis_map_inline:n absorb the token from the stream? Some peeks absorb, some leave it there, so I get a bit confused as to which is which. Dec 8, 2022 at 0:43
  • @AndrewStacey Glad it helps, and feel free to ask! Yes, it absorbs. It is a map function, so it goes over the items one by one, thus absorbing (deep semantics :). Note that the only code that is repeated for each token is the <inline code> of \peek_analysis_map_inline:n, and the code in this example does two things only: 1. check for the stop condition (a cs token \ifx-equal to \__loopspace_file_process_end:), and 2. process the token. In \peek_analysis_map_inline:n, looping is implied (map) and it only stops with \peek_analysis_map_break:. Dec 8, 2022 at 1:52

You must log in to answer this question.

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