我有一个带索引的二维数组和一个带字符的一维数组。以下代码绘制索引指示的字符,例如,(0,0) 处的字符将是“w”,(1,0) 处的字符将是 ldots,依此类推。
\documentclass[pdftex,twoside,a4paper,draft]{book}
% Tikz
\usepackage{tikz}
\usetikzlibrary{positioning, shapes, calc}
%
\begin{document}
\pagestyle{plain}
%
\begin{figure}
\begin{tikzpicture}[scale=1.0]
%character array (this works)
\def\mysymbols{{"w", "\ldots", "v", "w"}}
%
%character array with mathmode symbols in strings (this doesn't work: unknown operator '$')
%\def\mysymbols{{"$\approx$", "$\ddot{\ldots}$", "$\Upsilon$", "$\approx$"}}
%character array with mathmode symbols (this also doesn't work: unknown operator '7')
%\def\mysymbols{{$\approx$, $\ddot{\ldots}$, $\Upsilon$, $\approx$}}
%
%Array with indices
\def\myindices{{{0,1,0},{0,1,0},{2,2,0},{0,2,1}}}
%
\foreach \x in {0,...,2}{
\foreach \y in {0,...,2}{
%get symbol
\pgfmathparse{\mysymbols[\myindices[\y][\x]]}
%put a node at x,y with the symbol as the label
\node[] at ($(\x,\y)$) {\pgfmathresult};
}
}
\end{tikzpicture}
%
\end{figure}
%
\end{document}
现在,我遇到的问题是:我想在字符数组中使用数学模式符号,例如希腊字母。有没有办法在 tikz 数组中使用数学模式符号?
答案1
问题不在于数学。数学完全没问题,例如,你可以w
用"$w$"
(美元符号两边必须加双引号)替换。
问题出在数学重音上,例如\ddot
您的示例代码。出现此问题的原因是 TeX 扩展\ddot{\ldots}
为:
"$\mathaccent "707F\relax {\mathinner{\cdotp \cdotp \cdotp }}$"
您可以看到,字符串"707F
是"
TeX 用来作为十六进制数字前缀的转义符。在这种情况下,这"
会给解析器带来麻烦pgfmath
,它会认为双引号是字符串的结尾,因此后面7
是一个意外的标记。
通过将整个数学表达式包裹在一对附加括号中,可以轻松解决这个问题:"{$\ddot{\ldots}$}"
如下面的 MWE 所示:
\documentclass{article}
\usepackage{tikz}
\begin{document}
% Tikz
\usetikzlibrary{positioning, shapes, calc}
%
\pagestyle{plain}
%
\begin{figure}
\begin{tikzpicture}[scale=1.0]
\def\mysymbols{{"$\approx$", "{$\ddot{\cdots}$}", "$\Upsilon$", "$\approx$"}}
%Array with indices
\def\myindices{{{0,1,0},{0,1,0},{2,2,0},{0,2,1}}}
%
\foreach \x in {0,...,2}{
\foreach \y in {0,...,2}{
%get symbol
\pgfmathparse{\mysymbols[\myindices[\y][\x]]}
%put a node at x,y with the symbol as the label
\node[] at ($(\x,\y)$) {\pgfmathresult};
}
}
\end{tikzpicture}
%
\end{figure}
\end{document}