设置 tikzpicture 环境的默认字体

设置 tikzpicture 环境的默认字体

tikzpicture我想使用以下命令使环境中的所有文本都变为无衬线文本every node/.style={font=\sffamily}

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{backgrounds,fit,decorations.pathreplacing,calligraphy}
\usetikzlibrary{shapes.geometric}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}[inner sep = 0pt,
every node/.style={font=\sffamily}]

\node[regular polygon, regular polygon sides=6, draw, inner sep=1.2cm, shape border rotate=30] (q) at (0,0) {};
\node at (q.corner 1) (q1) {}; 
\node at (q.corner 4) (q4) {}; 
\node[font=\small,fill=white,align=center,minimum size=1.4cm,shift={(-.2,0)}] at (q.east) {subset \\ 2};

\node[regular polygon, regular polygon sides=6, draw, inner sep=2cm, shape border rotate=30] (p) at (0,0) {};
\node at (p.corner 1) (p1) {}; \node at (p.corner 2) (p2) {};
\node at (p.corner 3) (p3) {}; \node at (p.corner 4) (p4) {}; 
\node at (p.corner 5) (p5) {}; \node at (p.corner 6) (p6) {};

\draw (p2) -- (p5); \draw (p3) -- (p6);
\draw (p1) -- (q1); \draw (p4) -- (q4);

\node[circle,fill=white,draw=black,align=center,minimum size=.5cm] at (p1) {1}; 
\node[circle,fill=none,align=center,above] at (p1) {category 1};
\node[circle,fill=none,align=center,left] at (p3) {category 2};

\end{tikzpicture}

\end{document}

但结果图形中并非所有部分都使用无衬线字体。为什么会发生这种情况,我需要添加什么?(我只想sffamily在我的报告中使用此 tikzpicture 环境,所以我不能这样做

在此处输入图片描述

答案1

font=\sffamily为设定了tikzpicture,但后来使用 覆盖了该值font=\small。您可以使用该选项execute at begin node在节点开头执行任意代码,例如

every node/.style={execute at begin node={\sffamily}}

使用这个代替every node/.style={font=\sffamily}你应该没问题:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}
\begin{tikzpicture}[inner sep=0pt, every node/.style={execute at begin node={\sffamily}}]

\node[regular polygon, regular polygon sides=6, draw, inner sep=1.2cm, shape border rotate=30] (q) at (0,0) {};
\node at (q.corner 1) (q1) {}; 
\node at (q.corner 4) (q4) {}; 
\node[font=\small, fill=white, align=center, minimum size=1.4cm, shift={(-.2,0)}] at (q.east) {subset \\ 2};

\node[regular polygon, regular polygon sides=6, draw, inner sep=2cm, shape border rotate=30] (p) at (0,0) {};
\node at (p.corner 1) (p1) {}; \node at (p.corner 2) (p2) {};
\node at (p.corner 3) (p3) {}; \node at (p.corner 4) (p4) {}; 
\node at (p.corner 5) (p5) {}; \node at (p.corner 6) (p6) {};

\draw (p2) -- (p5); \draw (p3) -- (p6);
\draw (p1) -- (q1); \draw (p4) -- (q4);

\node[circle, fill=white, draw=black, align=center, minimum size=.5cm] at (p1) {1}; 
\node[circle, fill=none, align=center, above] at (p1) {category 1};
\node[circle, fill=none, align=center, left] at (p3) {category 2};

\end{tikzpicture}
\end{document}

在此处输入图片描述

当然,您也可以只选择\sffamily整个tikzpicture使用{\sffamily\begin{tikzpicture}...。\end{tikzpicture}}

相关内容