我希望图形中的字体是无衬线字体,并且比主文档小。我试过了
\begin{tikzpicture}[font=\sffamily]
\tikzstyle{every node}=[font=\small]
\draw (0,0) rectangle (5, 5) node[midway] {I am a rectangle}
\end{tikzpicture}
问题是,font=\small
图中的选项覆盖了\font=\sffamily
图片声明中的选项,最终我得到的是小衬线字体。如何同时设置字体大小和字体系列?
答案1
两种方式:
\sffamily
在输入之前调用tikz
,或者您可以在同一个指令中组合
\small
和。\sffamily
font=
这是 MWE
\documentclass{article}
\usepackage{tikz}
\begin{document}
{\sffamily
\begin{tikzpicture}
\tikzset{every node}=[font=\small]
\draw (0,0) rectangle (5, 5) node[midway] {I am a rectangle};
\end{tikzpicture}}
No longer in sffamily
\begin{tikzpicture}[font=\sffamily]
\tikzset{every node}=[font=\small\sffamily]
\draw (0,0) rectangle (5, 5) node[midway] {I am a rectangle};
\end{tikzpicture}
\end{document}
答案2
选项font
和node font
可组合:
\begin{tikzpicture}[font=\sffamily]
\draw[node font=\small]
(0,0) rectangle (4, 2) node[midway] {I am a rectangle};
\end{tikzpicture}
另外,add to font
可以定义一个选项,将额外的字体设置添加到选项的当前设置中,font
如下面的示例所示,其中包括上面的代码:
\documentclass{article}
\usepackage{tikz}
\makeatletter
% Definition of option "font" in "tikz.code.tex":
% \tikzoption{font}{\def\tikz@textfont{#1}}
% New TikZ option "add to font":
\tikzoption{add to font}{%
\providecommand*{\tikz@textfont}{}%
\expandafter\def\expandafter\tikz@textfont\expandafter{%
\tikz@textfont
#1%
}%
}
\makeatother
\begin{document}
\begin{tikzpicture}[font=\sffamily]
\draw[node font=\small]
(0,0) rectangle (4, 2) node[midway] {I am a rectangle};
\end{tikzpicture}
\begin{tikzpicture}[font=\sffamily]
\draw[add to font=\small]
(0,0) rectangle (4, 2) node[midway] {I am a rectangle};
\end{tikzpicture}
\end{document}