I prefer using figures created inside TeX, but I only know very labor-intensive methods. The following creates a permutation diagram for (6,3,2,4,1,5) with some colors and additional symbols.

I'm considering a project that would call for lots of these sorts of figures. Would it work to write a macro to create things like this? Any other advice for making this less painful?

\[ \begin{matrix}[cc|cccccc|c]  
& & & & & & {\color{blue}\downarrow} & {\color{blue}\downarrow} & S \\ \hline  
& 6 & {\color{red}\newmoon} & & & & & & {\color{red}\leftarrow} \\   
{\color{blue}*} &5 & & & & & &  {\color{blue}\newmoon} &  \\   
& 4 & & & & {\color{red}\newmoon} & & & {\color{red}\leftarrow} \\   
& 3 & & {\color{red}\newmoon} & & & & & {\color{red}\leftarrow} \\  
& 2 & & & \fullmoon & & & &  \\ 
{\color{blue}*} & 1 & & & & & {\color{blue}\newmoon}  & & \\ \hline
& & 1 & 2 & 3 & 4 & 5 & 6 & \\
&  & {\color{red}*} & {\color{red}*} & & {\color{red}*} & & &
\end{matrix} \]

enter image description here

  • The permutation diagram for (6,3,2,4,1,5) consists of the points (1,6), (2,3), (3,2), (4,4), (5,1), and (6,5). For purposes of this question, everything else is decoration (but decoration that I want to include). Commented Apr 21, 2013 at 0:29
  • And what are the rules of this decoration? (When blue/red/white, star/arrow?) Commented Apr 21, 2013 at 0:45
  • Two players, red and blue, alternate selecting dots. Red moves down the right column, blue moves right to left across the top row (S is for "strategic"). The last move after the state shown will color the white dot blue, put an arrow above it on top, and an asterisk next to the 2 on the left. This illustrates an algorithm for dividing indivisible items between two players when both know the other's preferences. Blue's ranking of the six items is 1, 2, 3, 4, 5, 6, red's is 6, 3, 2, 4, 1, 5, thus the permutation diagram. More at "Taking Turns," College Math. J. 41(4) 2010 pp298-297. Commented Apr 21, 2013 at 1:10
  • Wow, thanks very much for all of the helpful answers. Although I can only accept one, I learned from all of them. Commented Apr 21, 2013 at 20:57

5 Answers 5


I was tempted to use a matrix of nodes but my solution borrows from Jake's awesome code in Chinese checkers board using TikZ which effectively sets up a matrix of nodes manually.

This solution needs the tikz and the xstring packages.

The syntax

You use the code below as, for example




enter image description here

You'll notice that each part of the argument has three parts to it:

  • colour: this should be self explanatory
  • {list of coordinates}: note that this needs to be grouped in its own {}, and each ordered pair (x,y) needs to be written as x/y
  • left or right: this can either be left or right depending on how you want the legend to be displayed. Actually, it can either be left or anything else- if it is not left, it'll go on the right (and underneath).

The code

Here's a complete MWE that you can play with; the code is fairly detailed in its comments- it basically uses a few loops to set up the nodes, and then do the appropriate thing at each node (circle, arrow, or *).

% arara: pdflatex
% !arara: indent: {overwrite: true}


        % setup the nodes
        \foreach [evaluate=\i as \x using int(\i-1)]\i in {0,1,...,8}
            \foreach [evaluate=\j as \y using int(\j-1)] \j in {0,1,...,8}
                %      \node at (\i,\j)[name=perm-\x-\y,label=\x-\y]{};
                \node at (\i,\j)[name=perm-\x-\y,]{};

        % draw numbers 1 to 6 in both x and y direction
        \foreach \i in {1,...,6}
            \node at (perm-\i-0.center){\i};
            \node at (perm-0-\i.center){\i};
        \node at (perm-7-7.center){$S$};

        % vertical lines
        \draw ([xshift=-5mm]perm-1--1.south west)--([xshift=-5mm]perm-1-7.north west);
        \draw ([xshift=-5mm]perm-7--1.south west)--([xshift=-5mm]perm-7-7.north west);
        % horizontal lines
        \draw ([yshift=5mm]perm--1-0.south west)--([yshift=5mm]perm-7-0.south east);
        \draw ([yshift=5mm]perm--1-6.south west)--([yshift=5mm]perm-7-6.south east);

        % draw user input
        \foreach \mystyle/\coords/\leftorright in {#1}
            \foreach \x/\y in \coords
                \node[circle,fill=\mystyle,draw=\mystyle] at (perm-\x-\y){};
                    \node[\mystyle] at (perm--1-\y){*};
                    \node[\mystyle] at (perm-\x-7){$\downarrow$};
                {% otherwise put it on the right
                    \node[\mystyle] at (perm-7-\y){$\leftarrow$};
                    \node[\mystyle] at (perm-\x--1){*};






  • Nice solution too! The possibility to choose where the decorations will go seems also to be a good decision. Commented Apr 21, 2013 at 2:56

TikZ is not mandatory:-)

enter image description here


\put(\count@,\y){\if r\c\color{red}\else\if b\c\color{blue}\fi\fi
\if r\c
\if b\c




enter image description here

With the Asymptote module permdiag.asy typesetting of this nice little permutation game can be completely automated. Processing of the following file permdiag-test.asy

import permdiag;
permDiag pd=permDiag(new int[]{6,3,2,4,1,5});

for(int i=1;i<=pd.n;++i){

with asy -f pdf permdiag-test.asy results in 6 files diag01.pdf..diag06.pdf, which were combined together with


And this is the main permdiag.asy, which handles the permDiag class:

struct permDiag{
  int n;
  int[] perm;
  int[] dotState;
  int[][] tperm;

  picture boardPic;
  guide dotShape;
  pen[] dotFill;

  void drawNumbers(){
    for(int i=0;i<n;++i){

  void drawArrows(){
    for(int i=0;i<n;++i){

  void drawStars(){
    for(int i=0;i<n;++i){

  void drawLines(){

  void play(int hstep){
    int tmp;
    int redblu=0;
    for(int i=0;i<n;++i){
    for(int i=0;i<hstep;++i){
      for(int j=n-1;j>i;--j){

  picture board(int move){
    assert(move>=0 && move<=n);
                      //  move number, 0 = initial state, 
                      //   odd - after red move
                      //  even - after blu move
    for(int i=0;i<n;++i){       
    return boardPic;

  void operator init(int[] perm){
    this.dotFill=new pen[]{lightyellow,red,blue};
    this.tperm=new int[n][6];

//// Example:
//  import permdiag;
//  permDiag pd=permDiag(new int[]{6,3,2,4,1,5});
//  for(int i=0;i<=pd.n;++i){
//    shipout("diag"+format("%02d",i),pd.board(i));
//  }

Edit: Fixed picture scaling for different values of n. Example permdiag-test2.asy:

import permdiag;
permDiag pda=permDiag(new int[]{3,1,2,4});
permDiag pdb=permDiag(new int[]{9,19,17,1,8,13,18,11,10,4,5,7,2,3,15,16,12,6,20,14});
int i;

processed with asy -f pdf permdiag-test2.asy results in two pictures, diag-n4-03.pdf and diag-n20-12.pdf:

enter image description here

enter image description here

  • 2
    This is above & beyond and very impressive. Commented Apr 22, 2013 at 2:18

Here's a macro for you, also based on TikZ:




  % Loop for the first time over #1, printing the circles in
  % the desired style.
    \foreach[count=\i from 1] \val/\hue in {#1} {
        \draw (\i,\val) circle[radius = \circlerad];
        \fill[color=\hue] (\i,\val) circle[radius = \circlerad];
    % Now we know how many there are.  Loop again, printing the
    % arrows and stars at the edges.
    \foreach[count=\i from 1] \val/\hue in {#1} {
        % Blue above and below, red at the left and right
          \node at (\i,\count+1) {$\color{\bluename}\downarrow$};
          \node at (-0.5,\val)     {$\color{\bluename}*$};
          \node at (\count+1,\val) {$\color{\redname}\leftarrow$};
          \node at (\i,-0.5)         {$\color{\redname}*$};
    % Finally, print the frame: the axis values, the lines, and "S"
    \node at (\count+1,\count+1) {$S$};
    \foreach \i in {1,...,\count} {
      \node at (0,\i) {$\i$};
      \node at (\i,0) {$\i$};
      (-1,0.5) -- (\count+1.5,0.5)
      (0.5,-1) -- (0.5,\count+1.5)
      (-1,\count+0.5) -- (\count+1.5,\count+0.5)
      (\count+0.5,-1) -- (\count+0.5,\count+1.5);



enter image description here

The macro \permdiagram takes one mandatory argument, which is a comma-separated list giving the values (in order) of your permutation, each of which is optionally separated by a slash from its color (which must be either blue or red, but these are configurable by changing \bluename or \redname). If there's no slash, an empty circle is drawn; otherwise, a solid circle of that color is drawn. You can configure the radii via \circlerad, and you can set the scale of the whole picture by editing the x=1.5em,y=1.5em option to {tikzpicture}.

I chose not to do this as a matrix because it is way too sparse, as I'm sure you noticed when asking the question. Instead, I just place the circles directly at the required coordinates, after which I know how many there are and I can draw the stuff on the boundary.

  • Ah, this is nice! My answer seems useless now. Commented Apr 21, 2013 at 1:22

If you are asking if it possible to write something like


and obtain a similar picture, the answer is YES. However, the addidtional rules are needed. In particular, I thought that red circles mean numbers moved up, blue -- moved down, white -- unchanged, but it is not true, because 3 not 4 is white. If these rules are easy in terms of comparing numbers, making such a macro is an exercise.

For example (this is only a sketch, without graphics and with some simple rules)




\def\perm(#1,#2,#3){\pei#1 \peii#2 \peiii#3



enter image description here

  • @Werner Thank you for editing a picture. How could I achieve such a cut? Commented Apr 21, 2013 at 1:18
  • 2
    I recompiled the MWE, zoomed in on the important part, copy-and-pasted it using the regular "Insert image" interface (copy-and-paste have been made available since February 2013). See also How do you crop an attached PDF?
    – Werner
    Commented Apr 21, 2013 at 1:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .