
我想使用genealogytree
套件創建一個大型家譜樹,並決定將人員資料儲存在單獨的檔案中測試資料庫.tex(MWE 包含三個人)。從主文件中test.tex
,我建立了一種新格式genealogytree
:xTestFormat
使用「test_database.tex」中填入的鍵。
我閱讀了 pgf 手冊部分,\pgfkeys
並且了解如何在一般使用之前檢查金鑰是否存在,但在我的情況下則不然。來自 MWE:由於我事先不知道資料庫包含“Tom Water”數據,因此我不知道如何檢查comment
“Tom Walter”的金鑰是否存在。因此,當我嘗試使用該密鑰時,我收到錯誤訊息:Undefined control sequence. }
。
當我們事先不知道 pgfkey 的名稱時,如何檢查它是否不為空?
微量元素
(取消註解倒數第三行測試資料庫.tex以便編譯文件 ( comment = {There is nothing to say}
)。
測試文件
\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
定義來測試該資料庫條目是否存在,例如,使用\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}