# Every token between active chars to fixed hbox

I would like to type something like this:

|--1-23-|--4-5---|


and get such output that every | is a vertical line, every - appears like \rule[.8ex]{1em}{.5pt}, and all other (non-active) characters are in a hbox of the same width (e.g. \hbox to 1em{\hss\the\token\hss).

I can make a macro \fw that makes its argument to those hboxes of fixed width (as in the comment here: Can I turn a variable width font into a fixed-width font?). However, I'm struggling with how to call it without writing the argument with braces.

I made - to active character and tried to check whether the next character is also -. If yes, I just print the rule, and if not, I want to call \fw with an argument delimited by the next -. However, this doesn't work because 1) it takes the \fi at the end of if also as part of the argument, and that breaks things (at least that is what I understood from the log), 2) it consumes the final - and I might want to use it again for beginning of fixed width text, and 3) I'm confused about where to properly start and end groups.

Another option would be to use fixed width somehow on the whole line/paragraph, but that would also affect those |, and I also didn't find how to handle active characters here.

I was also thinking about using token register to read the tokens first and then decide what to do but I don't know how to use them and how to then decide what to do with which token.

Edit: I forgot to mention there might be also more instances of | inside of the word.

• look at \MakeShortVerb from the doc or verbatim package that lets you use |--12--3--4| you would just need to modify it slightly to re-add the rules Nov 17, 2021 at 14:44
• |--1-23-|--4-5---| - this can be taken for a leading |, trailed by an arbitrary sequence of |-delimited arguments.The question is: How to detect the last | ? Having TeX look ahead until reaching the end of the file? (An loop can be implemented which recursively gathers things in verbatim-catcode-régime until reaching either | or the end of the file. If reaching | process as as another |-delimited argument, probably retokenizing things using \scantokens and continue. If reaching the eof, pass to \scantokens for re-tokenization and processing as usual and terminate. ) Nov 26, 2021 at 14:56

This uses a tokencycle and assumes that the OP, as indicated, would like to literally type |--1-23---4-5---| and get the desired output. Of course, this is done by making | active, which restricts its use elsewise.

In this incarnation, groups and control sequences in the input are ignored.

\documentclass{article}
\usepackage{tokcycle,amsmath}
\catcode\|=\active
\def|#1|{$\lvert$%
\tokencycle
{\ifx-##1\rule[.8ex]{1em}{.5pt}\else\makebox[1em]{##1}\fi}
{}
{}
{\makebox[1em]{##1}}#1\endtokencycle
$\rvert$}
\begin{document}
This is |--1-23---4-5---| and done.
\end{document}


To avoid the use of an active | token, one may merely define \fw to take a delimited argument:

\documentclass{article}
\usepackage{tokcycle,amsmath}
\def\fw|#1|{$\lvert$%
\tokencycle
{\ifx-##1\rule[.8ex]{1em}{.5pt}\else\makebox[1em]{##1}\fi}
{}
{}
{\makebox[1em]{##1}}#1\endtokencycle
$\rvert$}
\begin{document}
This is \fw|--1-23---4-5---| and done.
\end{document}


Not that I recommend doing this, but it's possible.

\documentclass{article}

\ExplSyntaxOn

\cs_new_protected:Nn \zuz_textfw:
{
\mode_if_math:TF { \vert } { \__zuz_textfw:w }
}
\char_set_active_eq:NN | \zuz_textfw:

\AtBeginDocument
{
\char_set_catcode_active:N |
}

\cs_new_protected:Npn \__zuz_textfw:w
{
\peek_regex_replace_once:nn { [\- \| [:alnum:]]* } { \c{__zuz_textfw:n}\{|\0\} }
}

\cs_new_protected:Nn \__zuz_textfw:n
{
\tl_map_function:nN { #1 } \__zuz_textfw_char:n
}

\cs_new_protected:Nn \__zuz_textfw_char:n
{
\makebox[1em]
{
\str_case:nnF { #1 }
{
{|}{\textbar}
{-}{\rule[0.8ex]{\dim_eval:n { 1em-1pt }}{0.5pt}}
}
{#1}
}
}

\ExplSyntaxOff

\begin{document}

|--1-23-|--4-5---|

|----------------|

$\sqrt{x^2}=|x|$

\end{document}


The purpose of \zuz_textfw: is to discern whether we're in math mode or not; in the first case just issue \vert, otherwise \__zuz_textfw:w. This function looks for hyphens, bars or alphanumeric characters and replaces what's been found with

\__zuz_textwf:n { |<what's been found> }


This last function's job is to map every character and take the appropriate decision according on whether it's |, - or an alphanumeric character.

The character | is made active at begin document and assigned the meaning of \zuz_textfw:.

You can decide to augment the list of allowed characters, just add whatever you want in the search regular expression:

[\- \| [:alnum:]]*


means “find all sequence of characters consisting of |, - or alphanumeric characters and stop when you find no more of them”.

It would be much easier and more robust to use a syntax such as

\textfw{|--1-23-|--4-5---|}


so you don't have to play with active characters at all.

\documentclass{article}

\ExplSyntaxOn

\NewDocumentCommand{\textfw}{m}
{
\tl_map_function:nN { #1 } \__zuz_textfw_char:n
}

\cs_new_protected:Nn \__zuz_textfw_char:n
{
\makebox[1em]
{
\str_case:nnF { #1 }
{
{|}{\textbar}
{-}{\rule[0.8ex]{\dim_eval:n { 1em-1pt }}{0.5pt}}
}
{#1}
}
}

\ExplSyntaxOff

\begin{document}

\textfw{|--1-23-|--4-5---|}

\textfw{|----------------|}

\end{document}
`

The output is the same as in the other version.