仅更改图形的数学字体(包括图形内的 TikZ 环境)

仅更改图形的数学字体(包括图形内的 TikZ 环境)

在我的文档中,我希望主文本times和数学公式都使用txfonts字体。我可以借助该txfonts包来实现这一点。但是,我希望一切在我的图中显示为无衬线字体,包括数学。到目前为止,我已经能够将文本字体更改为无衬线字体,但我还想将数学字体更改为无衬线字体,无论是在标题中还是在图形本身中(我使用 TikZ)。

这是一个需要调整的最小(非工作)示例。问题在于图中的数学运算和标题,现在是txfonts

\documentclass{article}
\usepackage{tikz}
%This package is used to get sans serif for regular text in captions.
\usepackage[font=sf]{caption}
%This package is used to get times for the main text and txfonts for math.
\usepackage[varg]{txfonts}
\begin{document}
Main text is supposed to be times. Math in main text is supposed to be     txfonts: $1+1=3$.
\begin{figure}
%Here I invoke the font option to make text within TikZ sans serif.
\begin{tikzpicture}[font=\sffamily]
\node at (0,0) {Sans Serif}; 
\node at (0,1) {Sans Serif Math: $5=3$}; 
\end{tikzpicture}
\caption{Figure caption text is supposed to be sans serif. Math in figure   capture text is supposed to be sans serif: $3+1=4.5$.}
\end{figure}
\end{document}

答案1

您可以使用sansmath包装结合floateveryhook包仅更改图形中的数学。此解决方案继承了sansmath包实现的与 sans math 相关的所有问题。(有关详细信息,请参阅其文档。)

\documentclass{article}
\usepackage{tikz}
%This package is used to get sans serif for regular text in captions.
\usepackage[font=sf]{caption}
%This package is used to get times for the main text and txfonts for math.
\usepackage[varg]{txfonts}
\usepackage{float} % to easily modify floats
\usepackage{etoolbox} % nice command patching
\usepackage{sansmath} % sans serif math
\usepackage{everyhook} % nice \every... patching
% restyle figures to make \everymath=\sansmath (float package)
\restylefloat{figure}
\floatevery{figure}{\PushPreHook{math}{\sansmath}}
% undo the change to \everymath at the end of the figure (etoolbox)
\apptocmd{\endfigure}{\PopPreHook{math}}{}{}

\begin{document}
Main text is supposed to be times. Math in main text is supposed to be     txfonts: $1+1=3$.
\begin{figure}[h]
%Here I invoke the font option to make text within TikZ sans serif.
\begin{tikzpicture}[font=\sffamily]

\node[draw] at (0,0) {Sans Serif}; 
\node[draw] at (0,1) {Sans Serif Math: $5=3$}; 
\end{tikzpicture}
\caption{Figure caption text is supposed to be sans serif. 
 Math in figure   caption text is supposed to be sans serif: $3+1=4.5$.}
\end{figure}

This is some math after the figure $3 + 2 = 5$. It is back to serifs.
\end{document}

代码输出

答案2

您可以使用数学影响数学字体:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage[font=sf]{caption}
\usepackage[varg]{txfonts}
\begin{document}
With amsmath:
\[ \mathbf{bf\ Bold\ 3+5=8} \]
\[ \mathrm{rm\ Roman\ 3+5=8} \]
\[ \mathcal{cal\ Caligraphy\ 3+5=8} \]
\[ \mathsf{sf\ Sans\ Serif\ 3+5=8} \]
\[ \mathtt{tt\ Truetype\ 3+5=8} \]
\[ \mathit{it\ Italic\ 3+5=8} \]
\end{document}

还有 Blackboard Bold 字体(数学,用于表示数字系统)、Fraktur(数学) 和欧拉脚本 (手稿

例子

相关内容