帶有 tikzpicture 的主題標籤

帶有 tikzpicture 的主題標籤

我正在嘗試編譯下面的樹及其工作原理。問題是,每當我將 DP 或 LP 的標籤更改為\#P(ie \node(\#P) {\#P}; \#) 時,我都會收到以下錯誤訊息:

! Missing \endcsname inserted.
<to be read again>
\#
l.17 ...LP}; L [.YP Y [.FP ] ] ] ] ] ] ]
]
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.
Missing character: There is no # in font nullfont!
Missing character: There is no P in font nullfont!
\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{tikz-qtree-compat}

\tikzset{every tree node/.style={baseline=(top.base), level distance=2em, sibling distance=4em, align=center, parent anchor=south, child anchor=north, anchor=north}, sibling distance=15pt}

\usetikzlibrary{positioning} 

\begin{document}

\begin{tikzpicture}  [inv/.style={overlay, coordinate  }]  

\Tree [.XP X [.ZP Z [.\node(DP) {DP}; D    [.\node(LP) {LP}; L      [.YP Y [.FP     ] ] ] ] ]  ] ] ] 

\node [ right=1cm of DP,font=\itshape] (X) {Blah};

\draw[<-] (X.west) -- (DP);

\node [ right=1cm of LP,font=\itshape] (X) {Blah};

\draw[<-] (X.west) -- (LP);    

\end{tikzpicture}

\end{document}

輸出

答案1

#是 TeX 中的一個特殊字符,如果你想列印它,你必須轉義它。所以,你必須在普通文本中轉義它。但是,節點名稱不允許使用宏,因此不能\#在節點名稱中使用。

因此,我建議您使用另一個字母字符,例如在節點名稱中x替換\#(僅在代碼內部使用,無論如何都不會排版)。

請注意,即使是大多數「特殊字元 [...] 包括逗號、分號、連字符、大括號、點、圓括號、斜線、破折號等」(引用 TikZ手冊)不能用在節點名稱中。

關於更改\catcode,請參閱這裡


編輯可能的解決方案:1)只需將節點命名為與其文字不同的名稱即可; 2)如果節點名稱和節點文字必須相同,您可以使用巨集替換xD\#D

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}

\usepackage{xstring}
\newcommand{\xtohash}[1]{\StrSubstitute[0]{#1}{x}{\#}}

\begin{document}

\begin{tikzpicture}

\node at (0,0) (xD) {\#D};

\node at (0,1) (xD) {\xtohash{xD}};

\end{tikzpicture}

\end{document}

相關內容