检查定义未知 \pgfkeys

检查定义未知 \pgfkeys

我想使用创建一个大型家谱树genealogytree包创建一个大型家谱树,并决定将人员数据存储在单独的文件中测试数据库.tex(包含三个人的 MWE)。从主文件test.tex,我使用在“test_database.tex”中填写的键为创建一个新格式genealogytree: 。xTestFormat

我阅读了 pgf 手册部分,\pgfkeys并且我知道如何在使用密钥之前检查密钥是否存在,但在我的例子中却不知道。来自 MWE:由于我事先不知道数据库包含“Tom Water”数据,所以我不知道如何检查comment“Tom Walter”的密钥是否存在。因此,当我尝试使用密钥时,我收到了错误消息:Undefined control sequence. }

当我们事先不知道 pgfkey 的名称时,如何检查它是否为空?

平均能量损失

(取消注释倒数第三行测试数据库.tex该文件将被编译(comment = {There is nothing to say})。)

测试.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}

测试数据库.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

由于错误,Undefined control sequence. \gtrDBcomment您可以通过测试是否\gtrDBcomment定义来测试该 DB 条目是否存在,例如使用\ifdefined

如果您不知道,当 TeX 遇到未定义的控制序列时,它会抛出错误并显示一行上下文,在该行中,未定义的一个控制序列将是最后一个。在您的例子中,TeX 显示:

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

第一行的最后一个控制序列是\gtrDBcomment,这确实是 TeX 尝试扩展的控制序列,尽管它没有定义。

因此,如果您更改定义来\PrintPeople测试该宏是否已定义,那么您应该会很好:

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

    \familyname
    
    \ifdefined\gtrDBcomment
      \gtrPrintComment
    \fi
}

完成 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}

在此处输入图片描述

相关内容