7

Is there any trick that allows to have a { in argument delimiters of a macro ? for example, with this definition :

\def\start#1\fin{+++#1+++}

the call

\start coucou\fin{3}4

will display

+++coucou+++34

because \fin is the right delimiter of the argument of the macro \start. Now, i would like the whole \fin{3} to be the delimiter. So that the same call as above would now output:

+++coucou+++4

The problem is how to include the curly brace in the delimiters.

Edit1 In particular, I would like the following call

\start coucou\fin{2}blabla\fin{3} 

to "output"

coucou\fin{2}blabla

but, regarding to David's comments, it seems to be impossible.

Loic Rosnay
  • 8,167
  • It's impossible to have what you ask in the title, however it is of course possible to define a command that works as in your edit. Just define it as in my or egreg's version but of the argument after \fin _isn't 3 then just recurse and collect up to the next \fin and check again. This is how environments like tabularx or ams alignments that collect environments work, they scan up to \end check if it is the correct environment and if not collect up to the next \end. Oh I just realised \fin is probably \end if you're not english:-) – David Carlisle Jun 28 '12 at 23:36
  • can you make this in an expandable way ? I would appreciate a few lines of code. – Loic Rosnay Jun 29 '12 at 18:52
  • ok, i guess i could try to learn it in tabularx... – Loic Rosnay Jun 29 '12 at 19:05
  • well, it seems to me, that the idea you wrote in your comment above is precisely egreg's solution. Is it something more ? – Loic Rosnay Jun 29 '12 at 19:12
  • Sorry about that, I hadn't looked in detail at egreg's. I could delete my comment if you think it confusing or leave it, as it's true:-) – David Carlisle Jun 29 '12 at 20:06
  • "can you make this in an expandable way " yes egreg's is expandable – David Carlisle Jun 29 '12 at 20:11
  • no. nothing confusing now. Thanks for all the answers. :) And yes, \fin sounds like \end, you are right ! ;) – Loic Rosnay Jun 29 '12 at 20:35

3 Answers3

6

If the arguments to \fin are simple numbers, you can check whether the argument is 3; here's a Plain TeX version (put the standard things to make it into LaTeX).

\def\start{+++\startaux}
\def\startaux#1\fin#2{%
  #1%
  \ifnum#2=3
    +++%
  \else
    \fin{#2}%
    \expandafter\startaux
  \fi
}
\def\fin#1{-#1-}

\start cocou\fin{3}

\start cocou\fin{2}blabla\fin{3}

\bye

enter image description here

egreg
  • 1,121,712
  • yes, I think this is the answer I need :) By the way, what is \bye supposed to do ? for my TeX, it is just an undefined control sequence... – Loic Rosnay Jun 29 '12 at 20:38
  • @nicolasroy \bye is the Plain TeX way to end a job. – egreg Jun 29 '12 at 20:41
  • thank you for this wonderful solution! Could you briefly explain how the recursive part \expandafter\startaux works? – ValenStein Jul 12 '23 at 11:54
  • @ValenStein \expandafter will expand \fi (leaving nothing); this finishes up the conditional and \startaux will examine the next token. – egreg Jul 12 '23 at 12:22
3

No you can have \lbrace a command token that acts like { in some contexts and you can have a character token { of a different catcode such as 12 (other) but a token list (however constructed) can not have mis matched token of catcode 1 or 2.


So best you can do is something like:

\def\start#1\fin#2{%
\ifnum3=#2 +++#1+++\fi}
David Carlisle
  • 757,742
1

This gives the output that you want. I'm not sure that it will resolve whatever issue it is that led you to ask the question though.

\documentclass{article}
\def\start#1\fin{+++#1+++\fin}
\def\fin#1{}
\begin{document}
\start fish\fin{3}4
\end{document}
Scott H.
  • 11,047