How do you write a macro that takes arguments containing paragraphs?Add par only if last paragraph did not end with displayed mathHow do you define one macro with arguments inside anotherhow to define newcommand that takes [] as arguments and not ?TikZ Macro With Multiple ArgumentsHow can I pass expanded material to a macro at definition time (context-specific macro tracker)?Finding out the number of needed macro argumentsHow to create a macro that creates other macros (that takes arguments)?Macro that links JLS paragraphsHow to write a macro that takes a variable number of argumentsHow to create a table macro that takes more than 10 arguments

Why doesn't using two cd commands in bash script execute the second command?

What approach do we need to follow for projects without a test environment?

How do I hide Chekhov's Gun?

How to change two letters closest to a string and one letter immediately after a string using notepad++

Can a druid choose the size of its wild shape beast?

How to terminate ping <dest> &

Interplanetary conflict, some disease destroys the ability to understand or appreciate music

Co-worker team leader wants to inject his friend's awful software into our development. What should I say to our common boss?

Why did it take so long to abandon sail after steamships were demonstrated?

PTIJ: Who should I vote for? (21st Knesset Edition)

Have researchers managed to "reverse time"? If so, what does that mean for physics?

How could a scammer know the apps on my phone / iTunes account?

Sailing the cryptic seas

Recruiter wants very extensive technical details about all of my previous work

Why one should not leave fingerprints on bulbs and plugs?

Can I use USB data pins as power source

Could the Saturn V actually have launched astronauts around Venus?

What do Xenomorphs eat in the Alien series?

What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

Do I need life insurance if I can cover my own funeral costs?

If I can solve Sudoku can I solve Travelling Salesman Problem(TSP)? If yes, how?

Awsome yet unlucky path traversal

SOQL: Populate a Literal List in WHERE IN Clause



How do you write a macro that takes arguments containing paragraphs?


Add par only if last paragraph did not end with displayed mathHow do you define one macro with arguments inside anotherhow to define newcommand that takes [] as arguments and not ?TikZ Macro With Multiple ArgumentsHow can I pass expanded material to a macro at definition time (context-specific macro tracker)?Finding out the number of needed macro argumentsHow to create a macro that creates other macros (that takes arguments)?Macro that links JLS paragraphsHow to write a macro that takes a variable number of argumentsHow to create a table macro that takes more than 10 arguments













2















I'm trying to write a macro that takes arguments containing paragraphs. If you write a normal macro and one of its arguments contains a paragraph, it will break:



documentclassarticle
usepackage[utf8]inputenc

titletest

begindocument

maketitle

sectionIntroduction

defmymacro#1#1

mymacroThis

contains a paragraph

enddocument


This produces an error:




Runaway argument?
This
! Paragraph ended before mymacro was complete.
<to be read again>
par
l.15



So I tried to redefine par for expanding the arguments of the macro, but now it won't stop compiling:



documentclassarticle
usepackage[utf8]inputenc

titletest

begindocument

maketitle

sectionIntroduction

gdefoldparpar

defmymacrogdefparmymacroi

defmymacroi#1#1gdefparoldpar

mymacrofoo

bar

enddocument









share. A def, by default, doesn't allow a par token unless you explicitly say it's a longdef:

defmymacro#1#1


LaTeX, on the other hand, uses that by default, so if you use proper LaTeX commands (def shouldn't be used in LaTeX documents), newcommand makes a longdef by default. If you want a “short” def then you use newcommand*.



xparse returns the short argument default, but lets you define a long macro using the + argument modifier:



NewDocumentCommandmymacro m#1% def
NewDocumentCommandmymacro+m#1% longdef



Your second attempt is clever, and it could have worked except for two things.



First is that you are using gdefoldparpar and then gdefparoldpar. Once you expand par you get oldpar which, when expanded, yields par which, when expanded, yields oldpar which, when expanded, yields par which, when expanded, yields oldpar... Running forever :/



You need to use let (or globallet to have global effect) in this case: letoldparpar. This creates an exact copy of par named oldpar which does not depend on what is par.



Second, the runaway argument checking is implemented in a lower level, independent of the definition of par, so this would fail with the same error:



letparrelax
defmymacro#1#1
mymacrofoo

bar


because when TeX sees two endlinechar tokens (which is a space by default) TeX inserts an implicit par token, which raises the Runaway argument error. Knowing that, then:



newcountoldELchar
oldELchar=endlinechar
defmymacroendlinechar=-1relaxmymacroi
defmymacroi#1#1endlinechar=oldELchar
mymacrofoo

bar


won't raise an error, but a new line won't be a space anymore.






share|improve this answer













At the time TeX was written, one page of a document would take several minutes to be processed, and syntax highlighting was not a thing, so it was a good thing to have some mechanism to detect if you forgot a }. A def, by default, doesn't allow a par token unless you explicitly say it's a longdef:



defmymacro#1#1


LaTeX, on the other hand, uses that by default, so if you use proper LaTeX commands (def shouldn't be used in LaTeX documents), newcommand makes a longdef by default. If you want a “short” def then you use newcommand*.



xparse returns the short argument default, but lets you define a long macro using the + argument modifier:



NewDocumentCommandmymacro m#1% def
NewDocumentCommandmymacro+m#1% longdef



Your second attempt is clever, and it could have worked except for two things.



First is that you are using gdefoldparpar and then gdefparoldpar. Once you expand par you get oldpar which, when expanded, yields par which, when expanded, yields oldpar which, when expanded, yields par which, when expanded, yields oldpar... Running forever :/



You need to use let (or globallet to have global effect) in this case: letoldparpar. This creates an exact copy of par named oldpar which does not depend on what is par.



Second, the runaway argument checking is implemented in a lower level, independent of the definition of par, so this would fail with the same error:



letparrelax
defmymacro#1#1
mymacrofoo

bar


because when TeX sees two endlinechar tokens (which is a space by default) TeX inserts an implicit par token, which raises the Runaway argument error. Knowing that, then:



newcountoldELchar
oldELchar=endlinechar
defmymacroendlinechar=-1relaxmymacroi
defmymacroi#1#1endlinechar=oldELchar
mymacrofoo

bar


won't raise an error, but a new line won't be a space anymore.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 7 at 13:32









Phelype OleinikPhelype Oleinik

24.2k54688




24.2k54688












  • Haha, I was wondering where I went into infinite recursion...

    – sgf
    Mar 7 at 13:36











  • @sgf You can say, for instance, tracingall before the command you suspect is infinite-looping. You'll get a ton of garbage in the terminal and the log, but it gets easier to find out what is TeX doing.

    – Phelype Oleinik
    Mar 7 at 13:39











  • I would take a slight difference with the statement "(def shouldn't be used in LaTeX documents)". Certain service routines a user writes may require the special parsing provided by def syntax. I think it would be better to say "(def should be avoided, where possible)".

    – Steven B. Segletes
    Mar 7 at 13:44






  • 1





    the endlinechar version has to be used with care, for example it also removes the end of line after mymacro (even if there is text following mymacro) not just the one in the argument.

    – David Carlisle
    Mar 7 at 13:47






  • 1





    tex takes a line at a time and inserts the character specified by endlinechar at the point it grabs the next line at the end of the buffer before tokenizing., so its timing is well defined but er "delicate" :-)

    – David Carlisle
    Mar 7 at 13:59

















  • Haha, I was wondering where I went into infinite recursion...

    – sgf
    Mar 7 at 13:36











  • @sgf You can say, for instance, tracingall before the command you suspect is infinite-looping. You'll get a ton of garbage in the terminal and the log, but it gets easier to find out what is TeX doing.

    – Phelype Oleinik
    Mar 7 at 13:39











  • I would take a slight difference with the statement "(def shouldn't be used in LaTeX documents)". Certain service routines a user writes may require the special parsing provided by def syntax. I think it would be better to say "(def should be avoided, where possible)".

    – Steven B. Segletes
    Mar 7 at 13:44






  • 1





    the endlinechar version has to be used with care, for example it also removes the end of line after mymacro (even if there is text following mymacro) not just the one in the argument.

    – David Carlisle
    Mar 7 at 13:47






  • 1





    tex takes a line at a time and inserts the character specified by endlinechar at the point it grabs the next line at the end of the buffer before tokenizing., so its timing is well defined but er "delicate" :-)

    – David Carlisle
    Mar 7 at 13:59
















Haha, I was wondering where I went into infinite recursion...

– sgf
Mar 7 at 13:36





Haha, I was wondering where I went into infinite recursion...

– sgf
Mar 7 at 13:36













@sgf You can say, for instance, tracingall before the command you suspect is infinite-looping. You'll get a ton of garbage in the terminal and the log, but it gets easier to find out what is TeX doing.

– Phelype Oleinik
Mar 7 at 13:39





@sgf You can say, for instance, tracingall before the command you suspect is infinite-looping. You'll get a ton of garbage in the terminal and the log, but it gets easier to find out what is TeX doing.

– Phelype Oleinik
Mar 7 at 13:39













I would take a slight difference with the statement "(def shouldn't be used in LaTeX documents)". Certain service routines a user writes may require the special parsing provided by def syntax. I think it would be better to say "(def should be avoided, where possible)".

– Steven B. Segletes
Mar 7 at 13:44





I would take a slight difference with the statement "(def shouldn't be used in LaTeX documents)". Certain service routines a user writes may require the special parsing provided by def syntax. I think it would be better to say "(def should be avoided, where possible)".

– Steven B. Segletes
Mar 7 at 13:44




1




1





the endlinechar version has to be used with care, for example it also removes the end of line after mymacro (even if there is text following mymacro) not just the one in the argument.

– David Carlisle
Mar 7 at 13:47





the endlinechar version has to be used with care, for example it also removes the end of line after mymacro (even if there is text following mymacro) not just the one in the argument.

– David Carlisle
Mar 7 at 13:47




1




1





tex takes a line at a time and inserts the character specified by endlinechar at the point it grabs the next line at the end of the buffer before tokenizing., so its timing is well defined but er "delicate" :-)

– David Carlisle
Mar 7 at 13:59





tex takes a line at a time and inserts the character specified by endlinechar at the point it grabs the next line at the end of the buffer before tokenizing., so its timing is well defined but er "delicate" :-)

– David Carlisle
Mar 7 at 13:59











1














The argument of a macro defined with def does not allow par tokens. Neither it allows blank lines, because they're transformed to par during the phase in which TeX processes text input into tokens. Note that redefining par is useless in this respect, because it's precisely the token par that's disallowed, independently of its meaning.



Solution: make your macro long.



longdefmymacro#1#1


Better solution:



newcommandmymacro[1]#1


because newcommand uses longdef internally. The variant newcommand* instead uses def without the prefix.






share|improve this answer



























    1














    The argument of a macro defined with def does not allow par tokens. Neither it allows blank lines, because they're transformed to par during the phase in which TeX processes text input into tokens. Note that redefining par is useless in this respect, because it's precisely the token par that's disallowed, independently of its meaning.



    Solution: make your macro long.



    longdefmymacro#1#1


    Better solution:



    newcommandmymacro[1]#1


    because newcommand uses longdef internally. The variant newcommand* instead uses def without the prefix.






    share|improve this answer

























      1












      1








      1







      The argument of a macro defined with def does not allow par tokens. Neither it allows blank lines, because they're transformed to par during the phase in which TeX processes text input into tokens. Note that redefining par is useless in this respect, because it's precisely the token par that's disallowed, independently of its meaning.



      Solution: make your macro long.



      longdefmymacro#1#1


      Better solution:



      newcommandmymacro[1]#1


      because newcommand uses longdef internally. The variant newcommand* instead uses def without the prefix.






      share|improve this answer













      The argument of a macro defined with def does not allow par tokens. Neither it allows blank lines, because they're transformed to par during the phase in which TeX processes text input into tokens. Note that redefining par is useless in this respect, because it's precisely the token par that's disallowed, independently of its meaning.



      Solution: make your macro long.



      longdefmymacro#1#1


      Better solution:



      newcommandmymacro[1]#1


      because newcommand uses longdef internally. The variant newcommand* instead uses def without the prefix.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Mar 7 at 21:17









      egregegreg

      726k8819223230




      726k8819223230



























          draft saved

          draft discarded
















































          Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f478189%2fhow-do-you-write-a-macro-that-takes-arguments-containing-paragraphs%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

          Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

          How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page