TransWikia.com

Using a user-defined command to align cells in a NiceTabular environment (nicematrix package)

TeX - LaTeX Asked on August 9, 2021

I am typing a report in which I handle big tables using the NiceTabular environment to display data tables.
I would like to be able to set the alignment (l, c or r) of a given set of cells/blocks at the compilation (to allow the user to change them easily), and to have the other ones keeping a given alignment. Of course, at each compilation, I could go and change manually each one of the alignment commands in each of the block I want to change the alignment of. But my tables are pretty big, and that would take ages whenever I would like to change the alignment of these cells.

Hence, my idea would be to define a command MyAlignment that would apply to some cells/blocks only:

documentclass[12pt,a4paper]{article}
usepackage{nicematrix}

newcommand{MyAlignment}{r}

begin{document}

begin{NiceTabular}{cccc}
    Block[r]{1-1}{11} & Block[MyAlignment]{1-2}{12} & & 14 
    Block[MyAlignment]{2-1}{23} & Block[r]{1-2}{22} & & 24 
    & 32 & 33 & 34 
    41 & 42 & Block[MyAlignment]{1-2}{43} &
end{NiceTabular}

end{document}

However, when I try to compile this, the compiler returns "The key ‘MyAlignment’ is unknown for the command Block. If you go on, it will be ignored".
I tried to use def, edef, xdef and gdef instead of newcommand, it does not change anything.

Would you have any idea about whether it is possible to do that, and how to? I generally find answers to all of my questions on StackExchange, but this time, although I tried my best, I did not find any topic already tackling this question around.

Thank you so much!

One Answer

Here is a solution by defining a new command MyBlock.

documentclass[12pt,a4paper]{article}
usepackage{nicematrix}

newcommand{MyAlignment}{r}

ExplSyntaxOn
NewDocumentCommand { MyBlock } { O { } }
  { use:x { exp_not:N Block [ #1 ] } }
ExplSyntaxOff

begin{document}

begin{NiceTabular}{cccc}
    MyBlock[r]{1-1}{11} & MyBlock[MyAlignment]{1-2}{12} & & 14 
    MyBlock[MyAlignment]{2-1}{23} & MyBlock[r]{1-2}{22} & & 24 
    & 32 & 33 & 34 
    41 & 42 & MyBlock[MyAlignment]{1-2}{43} &
end{NiceTabular}

end{document}

Correct answer by F. Pantigny on August 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP