为什么不接受像和tikz
这样的字体命令内的多行文本?\texttt
\textbf
例如,编写文件
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
% this node works
\node[draw,align=left] (a) {Normal text\\works.};
% this node gives an error
\node[draw,align=left] [below of=a] {\texttt{Typewritter text is\\rejected.}};
\end{tikzpicture}
\end{document}
失败并显示错误
! Undefined control sequence.
\tikz@invoke@collected@onpath ...mmand \tikz@temp
\pgf@stop \tikz@node@is@a@...
l.10 ... {\texttt{Typewritter text is\\rejected.}}
这个问题有什么好的解决办法?
答案1
在节点属性中指定字体:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[draw,align=left] (a) {Normal text\\works.};
\node[draw,align=left,font=\ttfamily] [below of=a] {Typewriter text is\\not rejected.};
\end{tikzpicture}
\end{document}
答案2
添加text width
使线路可断开:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
% this node works
\node[draw,align=left] (a) {Normal text\\works.};
% this node gives an error
\node[draw,align=left, text width = 3cm] [below of=a] {\texttt{Typewriter text\\is not rejected.}};
\end{tikzpicture}
\end{document}