正确定义 Tikz 的字体系列

正确定义 Tikz 的字体系列

我有下面的图表,并且我遇到了一些困难,无法正确理解如何在 Latex 中管理字体。

<在此处输入图片描述

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{sansmath}
\usepackage{tgadventor}


\usepackage{adjustbox}


%%%%%%%%%%%%%%%%%% COLORS %%%%%%%%%%%%%%%%%%
\definecolor{orange}{RGB}{245,128,37}
\definecolor{bleu}{RGB}{0,0,255}

%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{pictparam/.style={font={\sansmath\sffamilly}, thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm, scale=0.75}}

\tikzset{fct/.style={thick, opacity=0.8, smooth, samples=200}}

%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
\begin{document}
\begin{adjustbox}{width={15cm},totalheight={15cm},keepaspectratio}


\begin{tikzpicture} [pictparam]

%%%% FUNCTIONS %%%%
% Ceci permet de déterminer les limites du repère
\newcommand{\xlab}{X}
\newcommand{\xmin}{-7}
\newcommand{\xmax}{3}

\newcommand{\ylab}{Y}
\newcommand{\ymin}{-3}
\newcommand{\ymax}{7}

\newcommand{\graduation}{5}   %graduation du tableau

%%%%%%%%%%%%%%%%%% Data Table %%%%%%%%%%%%%%%%%%

% Grille à placer en premier
\draw[gray, ultra thin] (\xmin,\ymin) grid (\xmax,\ymax);

% Axes à placer sur la grille
\draw[->] (\xmin-0.2,0) -- (\xmax+0.4,0);
\draw[->] (0,\ymin-0.2) -- (0,\ymax+0.4);
\draw (\xmax+0.8,0) node [below] {\footnotesize \xlab};
\draw (0,\ymax+0.8) node [left] {\footnotesize \ylab};

% Traits et valeurs sur les axes
\draw[color=black] (0,0) node[below left] {\footnotesize $0$}; % dot(0,0)

\foreach \x in {\xmin,...,-1}
    {   \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
        \draw[shift={(\x,0)},color=black] (0,0) node[below] {\footnotesize $\x$};
    }
\foreach \x in {1,...,\xmax}
    {   \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
        \draw[shift={(\x,0)},color=black] (0,0) node[below] {\footnotesize $\x$};
    }
\foreach \y in {1,...,\ymax}
    {   \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
        \draw[shift={(0,\y)},color=black] (0,0) node[left] {\footnotesize $\y$};
    }
\foreach \y in {\ymin,...,-1}
    {   \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
        \draw[shift={(0,\y)},color=black] (0,0) node[left] {\footnotesize $\y$};
    }

% Limitation des tracés dans la zone délimitée par la grille
\clip (\xmin,\ymin) rectangle (\xmax,\ymax);

% Tracés de fonctions
%% Question 2
% d'abord l'orange
\draw[orange, fct, domain=\xmin-1:\xmax] plot (\x+1,{(\x^(1/3))+3});
\node[orange] (h) at (0.6,1.6) {$h(x)$};
%\draw[orange] (0.3,1.6) node {$h$};;
 puis le bleu
\draw[bleu, fct, domain=\xmin-1:\xmax] plot (\x,{-(\x/7)^2 + (10/3)});
\node[bleu] (i) at (-4.4,2.4) {$i(x)$};
%%%%%%%%%%%%%%%%%% END %%%%%%%%%%%%%%%%%%
\end{tikzpicture}
\end{adjustbox}
\end{document}

我想将默认字体系列从 sansmath 更改为 tgadventor。我发现在加载“sansmath”包之前加载“tgadventor”包会应用字体更改。但我无法正确理解原因。

我发现我需要包“sansmath”来更改字体系列,但是阅读包手册并没有给我任何答案。

我最大的问题是:当我将 pdf 转换为 svg 图片时,字体系列更改丢失了。我不知道为什么,但我认为问题出在我更改字体系列的方式上

预先感谢您的帮助

答案1

我们来检查一下文档。手册sansmath说:

在声明范围内\sansmath,数学字符将尽可能从文本无衬线系列中取出。实际的无衬线字体是OT1那些由含义指示的编码\sfdefault 包裹装载时,不是每个数学环境中的含义!

因此,您必须更改默认的 sans-serif 字体,然后加载sansmath。之后,您可以将字体更改为您想要的任何字体,并且\sansmath仍将使用您之前选择的字体。

为了完整性,您有几个选择。该isomath软件包还为您提供了可选的无衬线数学字母。如果您使用的是 XeLaTeX,您可以尝试mathspec。最后,在 中unicode-math,您可以选择

\setmathfont[version=sans, Scale = MatchLowercase]{Fira Math}

\newcommand\sansmath{\mathversion{sans}}

不幸的是,截至 2020 年,unicode-math不允许您组合range=version=选项\setmathfont

修复 MWE

为了使其在 中运行pdflatex,请进行以下三处更改。该sansmath包需要希腊大写字母的 OT1 编码,因此除了 T1 之外还要加载它。

如上所述,sansmath加载时使用默认的无衬线字体,因此请先加载tgadventor

最后,有一个拼写错误。\sffamilly应该是\sffamily

这将为您提供以下工作MWE:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usepackage[OT1,T1]{fontenc}

\usepackage{tgadventor}
\usepackage{sansmath}

\usepackage{adjustbox}


%%%%%%%%%%%%%%%%%% COLORS %%%%%%%%%%%%%%%%%%
\definecolor{orange}{RGB}{245,128,37}
\definecolor{bleu}{RGB}{0,0,255}

%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{pictparam/.style={font={\sansmath\sffamily}, thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm, scale=0.75}}

\tikzset{fct/.style={thick, opacity=0.8, smooth, samples=200}}

%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
\begin{document}
\begin{adjustbox}{width={15cm},totalheight={15cm},keepaspectratio}


\begin{tikzpicture} [pictparam]

%%%% FUNCTIONS %%%%
% Ceci permet de déterminer les limites du repère
\newcommand{\xlab}{X}
\newcommand{\xmin}{-7}
\newcommand{\xmax}{3}

\newcommand{\ylab}{Y}
\newcommand{\ymin}{-3}
\newcommand{\ymax}{7}

\newcommand{\graduation}{5}   %graduation du tableau

%%%%%%%%%%%%%%%%%% Data Table %%%%%%%%%%%%%%%%%%

% Grille à placer en premier
\draw[gray, ultra thin] (\xmin,\ymin) grid (\xmax,\ymax);

% Axes à placer sur la grille
\draw[->] (\xmin-0.2,0) -- (\xmax+0.4,0);
\draw[->] (0,\ymin-0.2) -- (0,\ymax+0.4);
\draw (\xmax+0.8,0) node [below] {\footnotesize \xlab};
\draw (0,\ymax+0.8) node [left] {\footnotesize \ylab};

% Traits et valeurs sur les axes
\draw[color=black] (0,0) node[below left] {\footnotesize $0$}; % dot(0,0)

\foreach \x in {\xmin,...,-1}
    {   \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
        \draw[shift={(\x,0)},color=black] (0,0) node[below] {\footnotesize $\x$};
    }
\foreach \x in {1,...,\xmax}
    {   \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
        \draw[shift={(\x,0)},color=black] (0,0) node[below] {\footnotesize $\x$};
    }
\foreach \y in {1,...,\ymax}
    {   \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
        \draw[shift={(0,\y)},color=black] (0,0) node[left] {\footnotesize $\y$};
    }
\foreach \y in {\ymin,...,-1}
    {   \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
        \draw[shift={(0,\y)},color=black] (0,0) node[left] {\footnotesize $\y$};
    }

% Limitation des tracés dans la zone délimitée par la grille
\clip (\xmin,\ymin) rectangle (\xmax,\ymax);

% Tracés de fonctions
%% Question 2
% d'abord l'orange
\draw[orange, fct, domain=\xmin-1:\xmax] plot (\x+1,{(\x^(1/3))+3});
\node[orange] (h) at (0.6,1.6) {$h(x)$};
%\draw[orange] (0.3,1.6) node {$h$};;
 puis le bleu
\draw[bleu, fct, domain=\xmin-1:\xmax] plot (\x,{-(\x/7)^2 + (10/3)});
\node[bleu] (i) at (-4.4,2.4) {$i(x)$};
%%%%%%%%%%%%%%%%%% END %%%%%%%%%%%%%%%%%%
\end{tikzpicture}
\end{adjustbox}
\end{document}

如果您可以使用 LuaLaTeX 或 XeLaTeX,则可以执行以下操作:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usepackage{unicode-math}

\usepackage{adjustbox}

\defaultfontfeatures{Scale = MatchLowercase}
\setmainfont{Latin Modern Roman}[Scale = 1.0]
\setsansfont{Fira Sans}
\setmathfont{Latin Modern Math}
\setmathfont[version=sans]{Fira Math}

\newcommand\sansmath{\mathversion{sans}}

%%%%%%%%%%%%%%%%%% COLORS %%%%%%%%%%%%%%%%%%
\definecolor{orange}{RGB}{245,128,37}
\definecolor{bleu}{RGB}{0,0,255}

%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{pictparam/.style={font={\sansmath\sffamily}, thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm, scale=0.75}}

\tikzset{fct/.style={thick, opacity=0.8, smooth, samples=200}}

%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
\begin{document}
\begin{adjustbox}{width={15cm},totalheight={15cm},keepaspectratio}


\begin{tikzpicture} [pictparam]

%%%% FUNCTIONS %%%%
% Ceci permet de déterminer les limites du repère
\newcommand{\xlab}{X}
\newcommand{\xmin}{-7}
\newcommand{\xmax}{3}

\newcommand{\ylab}{Y}
\newcommand{\ymin}{-3}
\newcommand{\ymax}{7}

\newcommand{\graduation}{5}   %graduation du tableau

%%%%%%%%%%%%%%%%%% Data Table %%%%%%%%%%%%%%%%%%

% Grille à placer en premier
\draw[gray, ultra thin] (\xmin,\ymin) grid (\xmax,\ymax);

% Axes à placer sur la grille
\draw[->] (\xmin-0.2,0) -- (\xmax+0.4,0);
\draw[->] (0,\ymin-0.2) -- (0,\ymax+0.4);
\draw (\xmax+0.8,0) node [below] {\footnotesize \xlab};
\draw (0,\ymax+0.8) node [left] {\footnotesize \ylab};

% Traits et valeurs sur les axes
\draw[color=black] (0,0) node[below left] {\footnotesize $0$}; % dot(0,0)

\foreach \x in {\xmin,...,-1}
    {   \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
        \draw[shift={(\x,0)},color=black] (0,0) node[below] {\footnotesize $\x$};
    }
\foreach \x in {1,...,\xmax}
    {   \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
        \draw[shift={(\x,0)},color=black] (0,0) node[below] {\footnotesize $\x$};
    }
\foreach \y in {1,...,\ymax}
    {   \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
        \draw[shift={(0,\y)},color=black] (0,0) node[left] {\footnotesize $\y$};
    }
\foreach \y in {\ymin,...,-1}
    {   \draw[shift={(0,\y)},color=black] (2pt,0pt) -- (-2pt,0pt);
        \draw[shift={(0,\y)},color=black] (0,0) node[left] {\footnotesize $\y$};
    }

% Limitation des tracés dans la zone délimitée par la grille
\clip (\xmin,\ymin) rectangle (\xmax,\ymax);

% Tracés de fonctions
%% Question 2
% d'abord l'orange
\draw[orange, fct, domain=\xmin-1:\xmax] plot (\x+1,{(\x^(1/3))+3});
\node[orange] (h) at (0.6,1.6) {$h(x)$};
%\draw[orange] (0.3,1.6) node {$h$};;
 puis le bleu
\draw[bleu, fct, domain=\xmin-1:\xmax] plot (\x,{-(\x/7)^2 + (10/3)});
\node[bleu] (i) at (-4.4,2.4) {$i(x)$};
%%%%%%%%%%%%%%%%%% END %%%%%%%%%%%%%%%%%%
\end{tikzpicture}
\end{adjustbox}
\end{document}

您没有说您正在使用哪个 PDF 转换器,但更有可能将使用 OpenType 字体和 Unicode 映射表的 PDF 转换为使用相同字体的可选文本的 SVG。

如果没有,则表示您的编码器将恢复为默认字体,因此请寻找更改它的选项。您也可以让转换器将字母渲染为线条图。

答案2

离题了,因为使用该命令时的字体问题\sansmath已由@Davislor 答案(+1)解决。

对于我的练习:如何使用该图表pgfplots绘制\sansmath

在此处输入图片描述

\documentclass[margin=3mm]{standalone}
% when is used pgflatex
\usepackage[OT1,T1]{fontenc}
\usepackage{tgadventor}
\usepackage{sansmath}

% when is used xelatex
%\usepackage{unicode-math}
%\defaultfontfeatures{Scale = MatchLowercase}
%\setmainfont{Latin Modern Roman}[Scale = 1.0]
%\setsansfont{Fira Sans}
%\setmathfont{Latin Modern Math}
%\setmathfont[version=sans]{Fira Math}
%\newcommand\sansmath{\mathversion{sans}}

\usepackage{pgfplots}
\pgfplotsset{compat=1.16,
             width=15cm,height=15cm}
\usetikzlibrary{arrows.meta}
%---------------------------------------------------------------%
%%%% from https://tex.stackexchange.com/questions/
%%%%            15475/using-ifthenelse-in-pgfmath/15491#15491
\pgfmathdeclarefunction{ifthenelsefpu}{3}{\pgfmathparse{#1*#2 + !#1*#3}%
}
%---------------------------------------------------------------%

\begin{document}
    \begin{tikzpicture}
\sansmath % <==========  
%---------------------------------------------------------------%
\pgfmathdeclarefunction{f}{1}{%
\pgfmathparse{ifthenelsefpu(#1<0,-abs(#1)^(1/3)+3,#1^(1/3)+3)}}
\pgfmathdeclarefunction{g}{1}{\pgfmathparse{(\x/7)^2 + (10/3)}}
%---------------------------------------------------------------%
\begin{axis}[
    axis lines=middle,
    axis line style = {-Stealth, thick, shorten >=-3mm, shorten <=-3mm},
    grid=major,
    axis on top,
    xmin=-7, xmax=3,
    ymin=-3, ymax=7,
    xlabel={$x$},
    x label style={font=\large, xshift=3mm, anchor=west},
    ylabel={$f(x)$},
    y label style={font=\large, yshift=3mm, anchor=south},
    ticklabel style = {font=\footnotesize, inner sep=2pt, fill=white},
    samples at = {-7,-6,...,0,0.025,0.05,...,2,3},
            ]
\addplot [blue,   ultra thick]
    {g(x)}     node[pos=0.95,above] {$i(x)$};
\addplot [orange, ultra thick]
    {f(x-1)}    node[pos=0.95,above] {$h(x)$};
\end{axis}
    \end{tikzpicture}
\end{document}

答案3

对于 pgfplots,@Zarko,我解决了这样的问题: 在此处输入图片描述

主文本

%%%%%%%%%%%%%%%%%% INTRODUCTION %%%%%%%%%%%%%%%%%%
\documentclass[border=10pt]{standalone}

%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{xcolor}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tgadventor}

\usepackage{tikz}
\usepackage{pgfplots}

%%%%%%%%%%%%%%%%%% INPUT %%%%%%%%%%%%%%%%%%
\input{parameters.tex}
%\input{preamble.tex}

%%%%%%%%%%%%%%%%%% Variable %%%%%%%%%%%%%%%%%%
%   Axe x
\newcommand{\xmin}{-0.5}%                  Valeur minimum sur x
\newcommand{\xmax}{11}%                 Valeur maximum sur x
\newcommand{\xlab}{Notes des élèves}%    Titre de l'axe x
\newcommand{\xticks}{0}%                Nombre de graduations en x

%   Axe y
\newcommand{\ymin}{0}%                  Valeur minimum sur y
\newcommand{\ymax}{5}%                  Valeur maximum sur y
\newcommand{\ylab}{Nombre d'élèves}%   Titre de l'axe y
\newcommand{\yticks}{0}%                Nombre de graduation en y

%  Coordonnée des barres
%   (#1, #2) ==> 
%   #1=l'abscisse de la barre ; 
%   #2=L'ordonnée -(/hauteur) de la barre.

\newcommand{\listep}{(0, 0) 
                    (1, 1) 
                    (2, 2)
                    (3, 0)
                    (4, 2)
                    (5, 2)
                    (6, 3)
                    (7, 4)
                    (8, 3)
                    (9, 2)
                    (10, 2)
                    }
%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\pgfplotsset{
    compat=1.15,
    tick label style = {font=\sansmath\sffamily},
    every axis label = {font=\sansmath\sffamily},
    legend style = {font=\sansmath\sffamily},
    label style = {font=\sansmath\sffamily}
}
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
\begin{document}
\begin{tikzpicture}[font=\sansmath]
\begin{axis}[
            %height=5cm,
            %width=20cm,
            %enlargelimits=0.05,
            %enlarge x limits=false,
            yticklabels={-1,...,5},
            xticklabels={0,...,10}, 
            xtick={0,...,10}, 
            %xticklabel style={anchor=north},
            axis x line = bottom,
            axis y line = left,
            xlabel={\xlab},
            ylabel={\ylab},
            xmin=\xmin, xmax=\xmax,
            ymin=\ymin, ymax=\ymax,
            minor x tick num = \xticks,
            minor y tick num = \yticks,
            %area style,
            %xmajorgrids=true,
            %xminorgrids=true
            ymajorgrids=true,
            %yminorgrids=true,
            grid style=line,
            ]
                \addplot+[ybar, mark=no, orange, fill=orange!50!white] plot coordinates {\listep};

\end{axis}
\end{tikzpicture}

\end{document}

参数.tex

%%%%%%%%%%%%%%%%%% PARAMETRES %%%%%%%%%%%%%%%%%%
\author{Oscar.education}

%%%%%%%%%%%%%%%%%% COLORS %%%%%%%%%%%%%%%%%%
\definecolor{orange}{RGB}{245,128,37}
\definecolor{bleu}{RGB}{0,0,255}
\definecolor{gris}{RGB}{158,158,158}
\definecolor{vert}{RGB}{0,178,0}
\definecolor{rouge}{RGB}{255,0,0}
\definecolor{violet}{RGB}{190,0,120}

%%%%%%%%%%%%%%%%%% FONTS %%%%%%%%%%%%%%%%%%
%\setmainfont{sansmath}
%\setsansfont{sansmath}

%%%%%%%%%%%%%%%%%% OTHER %%%%%%%%%%%%%%%%%%
\setlength{\parindent}{0pt}
\setlength{\parskip}{2ex plus 0.5ex minus 0.2ex}
%\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
%\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
%\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

%%%%%%%%%%%%%%%%%% TIKZ %%%%%%%%%%%%%%%%%%
% Paramètres de l'image

%\tikzset{pictparam/.style={font={\Verdana}, thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm, scale=1}}      % non-fonctionnel, ne rajoute pas la police verdana à Tikz

\tikzset{pictparam/.style={font={\sansmath\sffamily}, thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm, scale=1}} 
    % \sansmath pour expressions math. sans empattement (sans serif)
    % \sffamily pour texte normal sans empattement

% Style pour le tracé de fonctions
\tikzset{fct/.style={thick, opacity=0.8, smooth, samples=200}}

% Style pour le remplissage de surfaces
\tikzset{surf/.style={gray!50!white,fill,opacity=0.6}}

% Style pour les points
\newcommand{\sommet}[4]
    {
        \draw[fill=orange] (#1,#2) circle (1.5pt);
        \draw[#3] (#1,#2) node {#4};
    }

% Style pour la base inférieure des solides
\tikzset{baseinf/.style={thick, top color=gray!50!black, bottom color=gray!10, middle color=gray, shading=axis, opacity=0.25}}
% Style pour la surface latérale des solides
\tikzset{corps/.style={thick, left color=gray!50!black, right color=gray!50!black, middle color=gray!50, shading=axis, opacity=0.25}}
% Style pour la base supérieure des solides
\tikzset{basesup/.style={thick, top color=gray!90, bottom color=gray!2, middle color=gray!30, shading=axis, opacity=0.25}}

相关内容