1

I want to create a large genealogy tree using genealogytree package and decided to store people data in a separate file test_database.tex (containing three people for a MWE). From the main file test.tex, I create a new format for genealogytree: xTestFormat using the keys that were filled in 'test_database.tex'.

I read the pgf manual section on \pgfkeys and I understand how to check if the key exist before using it in general but not in my case. From the MWE: since I do not know in advance that the database contains 'Tom Water' data, I do not know how to check that the key comment exists for 'Tom Walter'. So when I try to use the key, I got the error message: Undefined control sequence. }.

How to check that a pgfkey is not empty when we do not know its name in advance?

MWE

(Un-comment the third to last line in test_database.tex for the file to compile (comment = {There is nothing to say}).)

test.tex

\documentclass{standalone}

\usepackage[all]{genealogytree}

\begin{document}
    
    % Define new keys in genealogytree database
    \pgfkeys{/gtr/database/.cd,
        nationality/.store in=\nationality,
        given name/.store in=\givenname,
        family name/.store in=\familyname
    }
    
    % Define new command with information to print
    \newcommand\PrintPeople[1]{
        \gtrPrintSex~\givenname
    
        \familyname
        
        \gtrPrintComment
    }
    
    % Define new format for genealogytree
    \gtrDeclareDatabaseFormat{xTestFormat}
    {% Define tcolorbox parameters based on 'nationality' key
        \tcbset{french/.style={colback=blue!5!white,colframe=blue}}
        \tcbset{british/.style={colback=red!5!white,colframe=red}}      
    }
    {\PrintPeople}
    
    \input{test_database}
    
    \begin{tikzpicture}
        
        \genealogytree[
            timeflow=down,
            processing=database,
            database format=xTestFormat,
            box={\nationality}
        ]
        {child[id=fam_Water]{
            p[id=Justine]{persdata=JustineID}
            g[id=Tom]{persdata=TomID}
            c[id=Jane]{persdata=JaneID}
            }
        }
        
    \end{tikzpicture}
\end{document}

test_database.tex

% Create database

% Jane Water
\pgfkeys{/gtr/database/.cd,
    persdata/.is choice,
    persdata/JaneID/.style={
        female,
        nationality = british,
        given name  = Jane,
        family name = Water,
        comment     = {Something else}
    }
}

% Justine Random
\pgfkeys{/gtr/database/.cd,
    persdata/.is choice,
    persdata/JustineID/.style={
        female,
        nationality = french,
        given name  = Justine,
        family name = Random,
        comment     = {Something interesting}
    }
}

% Tom Water
\pgfkeys{/gtr/database/.cd,
    persdata/.is choice,
    persdata/TomID/.style={
        male,
        nationality = british,
        given name  = Tom,
        family name = Water,
%       comment     = {There is nothing to say}
    }
}

1 Answer 1

1

Since the error is Undefined control sequence. \gtrDBcomment you can just test whether that DB entry exists by testing whether \gtrDBcomment is defined, e.g., with \ifdefined.

In case you didn't know, when TeX encounters an undefined control sequence, it throws the error and shows a line of context, in that line the one control sequence that wasn't defined will be the last one. In your case, TeX shows:

Undefined control sequence.
\gtrPrintComment ... {0pt}{\itshape \gtrDBcomment
                                                  }

The last control sequence of the first line is \gtrDBcomment, and that was indeed the control sequence TeX tried to expand though it wasn't defined.

So if you change the definition of \PrintPeople to test whether that macro is defined, you should be good:

\newcommand\PrintPeople[1]{
    \gtrPrintSex~\givenname

    \familyname
    
    \ifdefined\gtrDBcomment
      \gtrPrintComment
    \fi
}

Complete MWE:

\documentclass{standalone}

\usepackage[all]{genealogytree}

\begin{document}

% Define new keys in genealogytree database
\pgfkeys{/gtr/database/.cd,
    nationality/.store in=\nationality,
    given name/.store in=\givenname,
    family name/.store in=\familyname
}

% Define new command with information to print
\newcommand\PrintPeople[1]{
    \gtrPrintSex~\givenname

    \familyname
    
    \ifdefined\gtrDBcomment
      \gtrPrintComment
    \fi
}

% Define new format for genealogytree
\gtrDeclareDatabaseFormat{xTestFormat}
{% Define tcolorbox parameters based on 'nationality' key
    \tcbset{french/.style={colback=blue!5!white,colframe=blue}}
    \tcbset{british/.style={colback=red!5!white,colframe=red}}      
}
{\PrintPeople}

% Contents of the database
% Jane Water
\pgfkeys{/gtr/database/.cd,
    persdata/.is choice,
    persdata/JaneID/.style={
        female,
        nationality = british,
        given name  = Jane,
        family name = Water,
        comment     = {Something else}
    }
}
% Justine Random
\pgfkeys{/gtr/database/.cd,
    persdata/.is choice,
    persdata/JustineID/.style={
        female,
        nationality = french,
        given name  = Justine,
        family name = Random,
        comment     = {Something interesting}
    }
}
% Tom Water
\pgfkeys{/gtr/database/.cd,
    persdata/.is choice,
    persdata/TomID/.style={
        male,
        nationality = british,
        given name  = Tom,
        family name = Water,
%       comment     = {There is nothing to say}
    }
}
% End of database

\begin{tikzpicture}
    
    \genealogytree[
        timeflow=down,
        processing=database,
        database format=xTestFormat,
        box={\nationality}
    ]
    {child[id=fam_Water]{
        p[id=Justine]{persdata=JustineID}
        g[id=Tom]{persdata=TomID}
        c[id=Jane]{persdata=JaneID}
        }
    }
    
\end{tikzpicture}
\end{document}

enter image description here

1
  • 1
    Thanks, it also works if there is no family name. That's perfect!
    – AlMa
    Nov 8, 2020 at 16:22

You must log in to answer this question.

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