0

I'd like to know if it's possible to make a LaTex command that does the following:

The parameters to this command are 1) some integer (we'll call it n, for reference), 2) a color name, 3) the name of some user-supplied command to be invoked at a later time, as well as any (possibly optional) parameters to the command.

The context of this command would be that it is always expected to be used at the beginning of a row in a table, and what it would do is invoke a \rowcolor right where it was, and would also invoke the same \rowcolor for all rows following it, creating a block of rows that all have the same background color starting with the row where the command is invoked. However, after coloring n additional rows, the custom command with its parameters is then invoked at the start of the next row, (which may, in turn, invoke this new command again). If the color parameter is empty, then no background color should be used. If the command parameter is empty, then it should just stop coloring after the n+1 rows are colored.

If one invokes the block coloring command at the beginning of some row while it is still inside of some colored block, it should immediately abort any row counting it was doing for that block (as well as not call the command that may have been supplied), and just start a new one.

There should also be a second command that one could use at the beginning of rows of a table header of a longtable, for example, which would temporarily suspend row counting for the current block and not attempt to color the whole row that wold otherwise occur (although this command could be followed by a \rowcolor of its own, if one wanted to color the header row differently, for example).

These two commands could then be easily used to define arbitarily sized blocks of background color, and have any number of colors being used within that table, defining each colored block they want to use in sequence, specifying it to invoke the next one upon completion, and then possibly cycling back to the first one.

One would then simply invoke the command for the first group at the start of the row where they wished to start coloring, and then because one could define each color block to call the next one in sequence upon completion, you could create a cycle of 2, 3, or however many different background colors you wanted, used throughout the table, and each colored block could have a custom size (not necessarily just one row). Using the second command mentioned above, you could easily have the headers of a multi page table colored differently (or uncolored), and it would not otherwise affect the coloring of the rows throughout the rest of the table.

So what I could do is something like the following (profuse apologies if I've messed up any syntax.... I'm still very much learning TeX and LaTex, and am still completely new to the notion of defining my own commands):

\newcommand{\colorgroupone}{\blockColor{2}{}{\colorgrouptwo}}
\newcommand{\colorgrouptwo}{\blockColor{1}{lightgray}{\colorgroupthree}}
\newcommand{\colorgroupthree}{\blockColor{0}{gray}{\cologroupone}}

(I only hope I haven't messed up the syntax so badly that I've inadverently obfuscated what I am trying to do)...

Anyways, then one could, at the beginning of some row in the table, use the \colorgroupone command, that would then cycle through three background colors, the first three lines being having no background color, then 2 lines of light gray, followed by 1 line of a darker gray, and then cycle back to the first group again.

  • 1
    Have a look at \rowcolors in the xcolor package. It does not do all of what you are asking for but it does provide a way of adding background color to rows, in repeating blocks mod some n. The difference to what you are asking for is that each time you want the colours to change you need to add another \rowcolors command. – Andrew Oct 15 '17 at 20:48
  • I've tried, thanks. I had tried to read the source to \rowcolors to see if I could figure out how to do it from that, but it seems that it it is hard coded to switch only between two colors, and even then, alternate only every other line, so no customizable sized blocks of color. Also, I've been having difficulty understanding the flow of the command. Again, I'm very new to TeX, and can't say I can fully read how the command works in the LaTeX code. :/ – Mark Oct 15 '17 at 21:02
  • 1
    @Mark for a block you could use \rowcolors with two times the same color... – Skillmon Oct 15 '17 at 21:15
  • @Skillmon how would I do that, exactly? I didn't see any way in the command to make it do a block. – Mark Oct 15 '17 at 22:23

Your Answer

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

Browse other questions tagged or ask your own question.