In my document, I defined a macro \newcommand{\ctft}[1]{\mathscr{F}#1} for applying continuous time Fourier transform. An example in my code that uses the ctft command is \ctft{\delta_{a}}. Later on, for generality, I created a new macro \newcommand{\application}[2]{{#1}{#2}} to represent the idea of applying a function and a new macro \newcommand{\ctftop}{\mathscr{F}} to represent the operator for continuous time Fourier transform. So now, I would like to change all forms of \ctft{something} into \application{\ctftop}{something}. I tried making this modification using the regex \\ctft\{(.*?)\}, but failed: this regex would capture \ctft{\delta_{a}, instead of \ctft{\delta_{a}}. So it seems that the regex has to be able to ensure equal numbers for delimiters { and }. How can I make this happen using regex?

  • 3
    That's a question about the editor, rather than TeX.
    – egreg
    Feb 14 at 21:23
  • you can not mach arbitrary nested braces with a regex (that's essentially what the "regular" in "regular expression" means) but why do you ned a regex, don't you just want to change \ctft{ to \application{\cftop}{ ? Feb 14 at 21:27
  • your \application macro seems wrong, \application{\sin}{x} would produce {\sin}{x}not \sin {x} so would lose the operator spacing for \sin Feb 14 at 21:31
  • @DavidCarlisle - If we're talking about balanced nested braces, you can try putting \ctft{\delta_{a}} in a .tex file, do M-x re-builder in your favorite editor and yank this snippet for the regexp string: \\\\ctft{\\([^}{]*\\({[^}{]*}[^}{]*\\)*[^}]*\\)}. Things get complicated once you have escaped braces. Feb 15 at 11:39
  • @ArashEsbati you can match braces to any fixed depth, that one if I read it right matches {} containing possible {} groups that don't contain braces. but regex can not count (some systems provide non-regular extensions that can match braces) so you can not match {{{{a}b}c}} with the one you show. Feb 15 at 12:22

1 Answer 1


You do not need a regex here, just replace \ctft{ by \application{\cftop}{

  • My bad... I only thought of using a general tool, but didn't think of this simple solution :)
    – Ziqi Fan
    Feb 14 at 21:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.