I've done a few searches, and found possible solutions to my question. However, those that I saw were somehow more complicated than what I think I really need. So please excuse me for asking again. Here's the scenario:

I want to define a math command that gives the long exact sequence of the given parameters. So for example, if I write \longexactsequence{A}{B}{C}{D} then I expect to have something like 0 \longrightarrow A \longrightarrow B \longrightarrow C \longrightarrow D \longrightarrow 0.

Similarly, if I just write \longexactsequence{A}, then I would just get 0 \longrightarrow A \longrightarrow 0.

How can I do this? I'm also pushing this one step further, as I would doubly appreciate a "fix" for breaking the line if the output equation is already too long.

Thanks a lot, and all comments, hints, etc would be greatly appreciated!

  • 2
    This can be done using \@ifnextchar\bgroup. It may be easier and more robust to simply use a , to separate the entries: \longexactsequence{A,B,C,D}. – Qrrbrbirlbel Jul 30 '13 at 23:33
  • Hi there. Actually, it makes more sense to follow your suggestion. I'm now searching for this. Thanks! =) – Eric Jul 30 '13 at 23:34
  • 1
    Following @Qrrbrbirlbel's suggestion of using a CSV list, your could just use solutions from Iterating through comma-separated arguments. – Werner Jul 31 '13 at 0:08

An implementation using \kernel@ifnextchar for an input syntax like \longexactsequence{A}{B}{C}{D} and an implementation using LaTeX’s \@for macro for an input syntax like \longexactsequence{A, B, C, D} can be found in the history of this answer.

When you want to use , as a separator for the entries, you can use one of many packages that provide loops. One of this is the quite powerful \foreach macro of PGF, it has its own tag: .

If you want to apply a second value for a superscript of the arrow you can use \foreach, too, but need to use / for the delimiter of those arguments.

An arrow that should not be labeled need to be input as /<entry>.

There are other ways to input this, say

$ \longexactsequence{[f]A, [g]B, C} $

making the arrow label optional but this will need a more sophisticated implementation.

A poor man’s version of this has been implemented by using \ifx that checks whether both following macros are equal (which \superscript and \entry are if you don’t provide the /). This can be surpassed by adding another space (D/ D), an empty group (D{}/D) or \relax (D\relax/D) in one of both parameters.

The examples should show this clearly.


  #1 \longrightarrow
  \foreach \superscript/\entry in {#2} {%
    \entry \xlongrightarrow{\ifx\superscript\entry\else\superscript\fi}%
  } #1%

$ \longexactsequence{A, B, C, D, E} $

$ \longexactsequence{f/A, g/B, C, D/ D} $ % a space before or after / makes the Ds unequal

$ \longexactsequence[Z]{a/A, {a, b}/B} $

$ \longexactsequence{} $


enter image description here

| improve this answer | |
  • Great! With your explanation, it becomes clear now. I really appreciate this! Thanks! =) – Eric Jul 30 '13 at 23:43
  • 1
    @Eric The [2][0] specify for the definition of \longexactsequence that it takes two (2) arguments, the second pair of brackets imply that the first of those two arguments is optional, the default value is 0. In the A implementation you cannot have 0 → 0 (if this is allowed at all). The B one survives an empty argument (\longexactsequence{}) and prints correctly 0 → 0. If an empty set is to be accepted often, one can change the definition of A easily, if needed. – Qrrbrbirlbel Jul 30 '13 at 23:50
  • 1
    @Eric Though, in the end I recommend B and even more so C because you can quite easily create and customize loops (there are a lot of packages and tools for looping in LaTeX, PGF’s \foreach is simply the one I am most comfortable with), the implementation is also the shortest. – Qrrbrbirlbel Jul 31 '13 at 0:01
  • I prefer Code B more for my needs. Also, I don't think I would be having 0 \longrightarrow 0. Last question, is there a way for the \@for to take two parameters at a time? I mean, I wanted to extend this code to have something like \longexactsequence{A,B}{f,g} to mean 0\longrightarrow A \xlongrightarrow{f} B \xlongrightarrow{g} 0? (I am really a newbie to latex codes.) – Eric Jul 31 '13 at 0:02
  • 1
    @Eric Congratulations! Nevertheless, I have updated and cleaned up my answer to show you the easiest \foreach implementation I could think of. – Qrrbrbirlbel Jul 31 '13 at 0:28

The \getargsC macro parses a single argument into "words" that are placed in \argi, \argii, \argiii, \argiv, etc. The total number of words are set in the string \narg. A word with spaces or ending in a macro, can be isolated by surrounding it in braces (i.e., the contents of the inner braces becomes a word. Then I just regurgitate the words one by one, with the proper decorations surrounding them.

In the loop, \csname arg\roman{index}\endcsname just becomes successive arguments (\argi, \argii, \argiii, \argiv)

  \whiledo{\value{index} < \narg}{%
    \csname arg\roman{index}\endcsname\longrightarrow%
\longexactsequence{A B C D}

To show with other than simple characters

\longexactsequence{A_1 {\alpha} {B + C}}

enter image description here

| improve this answer | |
  • I'm voting for this answer and not the other one because it uses code that hides kernel internals from the innocent. – Ryan Reich Jul 30 '13 at 23:50
  • Thanks! I have to admit though that it would take me more time to study and understand what's happening here. =) – Eric Jul 30 '13 at 23:51
  • 1
    @eric I just added a short explanation. – Steven B. Segletes Jul 30 '13 at 23:54
  • But can this solution handle two sequences of arguments, similar to how the accepted solution can? – user89 Sep 24 '17 at 18:44
  • @user89 As is, no it cannot. However, the listofitems package, from which the readarray package draws, can do what you ask in a quite straightforward manner. If I get a chance, I might try to work something up. – Steven B. Segletes Sep 25 '17 at 2:49

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.