我正在寻找一种方法来调整 tikz 图像的大小,而不改变线条粗细、字体大小等。使用参数时,它可以像 pgfplots 一样工作width=15cm, unit vector ratio=1 1 1,
。
我已经尝试过了:
- 规模
- 调整框
目前最有效的方法是改变参数x=1,92cm, y=1,92cm
,使\begin{tikzpicture}
图像大致匹配预期大小,而不改变线条粗细或字体大小。有没有人有更好的方法?
主文本
%%%%%%%%%%%%%%%%%% INTRODUCTION %%%%%%%%%%%%%%%%%%
\documentclass[border=10pt]{standalone}
%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{tikz}% permet de dessiner des figures, des graphiques
\usepackage{adjustbox}% permet de déterminer une taille de fenêtre
%% FONT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tgadventor}% paquet de police de caractère TGadventor
\usepackage{sansmath}% Copie-colle la police active dans \sfdefault (/!\ N'EST PAS UNE POLICE DE CARACTÈRES)
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
\begin{document}
%\begin{adjustbox}{width={15cm},totalheight={15cm},keepaspectratio}
\begin{tikzpicture}[font={\sansmath\sffamily\Large}, line width=0.4mm, line cap=round, line join=round, >=latex, x=1.92cm, y=1.92cm,]
\draw[->] (-5-.4,0) -- (2+.4,0);
\foreach \x in {-5,...,2}
{ \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
}
\foreach \x in {-1,1}
{ \draw[shift={(\x,0)},color=black] (0,0) node[inner sep=5pt, above] {$\x$};
}
\draw[fill=orange]
(-2.9,0) coordinate (O) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$E$}
(-5,0) coordinate (A) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$F$}
(1.4,0) coordinate (B) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$G$}
(-2,0) coordinate (C) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$H$};
\end{tikzpicture}
%\end{adjustbox}
\end{document}
答案1
你能使用pgfplots
它,从而设置宽度。这是您的图及其版本pgfplots
。如果您制作了许多这样的图,我认为您会更好,pgfplots
因为您可以将所有这些键存储在一个样式中。
%%%%%%%%%%%%%%%%%% INTRODUCTION %%%%%%%%%%%%%%%%%%
\documentclass[tikz,border=10pt]{standalone}
%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{adjustbox}% permet de déterminer une taille de fenêtre
%% FONT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tgadventor}% paquet de police de caractère TGadventor
\usepackage{sansmath}% Copie-colle la police active dans \sfdefault (/!\ N'EST PAS UNE POLICE DE CARACTÈRES)
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
\begin{document}
%\begin{adjustbox}{width={15cm},totalheight={15cm},keepaspectratio}
\begin{tikzpicture}[font={\sansmath\sffamily\Large}, line width=0.4mm, line cap=round, line join=round, >=latex, x=1.92cm, y=1.92cm,]
\draw[->] (-5-.4,0) -- (2+.4,0);
\foreach \x in {-5,...,2}
{ \draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
}
\foreach \x in {-1,1}
{ \draw[shift={(\x,0)},color=black] (0,0) node[inner sep=5pt, above] {$\x$};
}
\draw[fill=orange]
(-2.9,0) coordinate (O) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$E$}
(-5,0) coordinate (A) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$F$}
(1.4,0) coordinate (B) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$G$}
(-2,0) coordinate (C) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$H$};
\end{tikzpicture}
%\end{adjustbox}
\pgfplotsset{Nilcouv/.style={width=16.5cm,
axis y line=none,axis x line=center,
xticklabel={\pgfmathtruncatemacro\itest{(\tick==1 ? 1 : (\tick==-1 ? -1 : 0))}%
\unless\ifnum\itest=0\relax $\itest$\fi},xtick distance=1,
every inner x axis line/.append style={line width=0.4mm, line
cap=round,-latex},x tick label style={above=4pt},
major tick length=6pt,every tick/.style={thick},
every node near coord/.append style={anchor=north,yshift=-2pt,execute at begin node=$,
execute at end node=$},enlarge x limits={rel=0.15}}}
\begin{tikzpicture}[font={\sansmath\sffamily\Large}, line join=round]
\begin{axis}[Nilcouv]
\addplot[nodes near coords,only marks,mark=*,
mark options={fill=orange,draw=black,thick},
point meta=explicit symbolic]
table [meta=label,y expr=0] {
x label
-2.9 E
-5 F
1.4 G
-2 H
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
以下内容基于我的答案在这里,它本身基于来自 marmot 的代码。其理念是利用自动化、迭代的过程来确定所需的规模。
下面给出的代码定义了一个名为autoscale autoid
(in /tikz
) 的样式。当tikzpicture
使用时autoscale autoid={width}{height}
,在完成后测量其边界框,并根据边界框的大小和目标尺寸计算比例因子。此比例因子写入文件.aux
,并与自动生成的标识符相关联。下次编译图片时,由于保存了比例因子,键/tikz/scale
会自动应用于 ,tikzpicture
以使其尽可能大,不超过规定的宽度和高度,同时保持其纵横比。
就你的情况而言,由于你唯一的限制是宽度,因此你可以\maxdimen
使用高度风格的论点autoscale autoid
;这有效地消除了对高度的限制。
当使用计算出的比例重新编译图片时,它通常会接近“理想”尺寸,但不会确切地第一次运行后,图像的大小就变成这个样子。这是因为标签等因素不受密钥的影响/tikz/scale
。但是,这个过程通常收敛得相当快,而且在仅仅两三次编译运行后,结果就已经非常出色了。在极端情况下,您可能需要删除该.aux
文件,以便重新开始使用样式对每张图片进行 1 级缩放autoscale autoid
。
下面的示例使用autoscale autoid={15cm}{\maxdimen}
您环境的选项tikzpicture
,并在图片上方绘制一条 15 厘米长的橙色规则,以便人们轻松评估结果。
\documentclass{article}
\usepackage[hmargin=2cm]{geometry} % for your 15 centimeters wide figure
\usepackage{sansmath} % only needed for your particular figure
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}
\makeatletter
\ExplSyntaxOn
\msg_new:nnn { nilcouv } { duplicate-figure-id }
{ duplicate~figure~identifier:~'#1'. }
% Sequence recording all figure identifiers (for the 'scale to max size' TikZ
% style) found so far
\seq_new:N \g__nilcouv_scale_to_max_style_figure_ids_seq
% Counter used when generating automatic figure identifiers for 'autoscale'
\int_new:N \g_nilcouv_last_autogenerated_figure_nb_int
\cs_new_protected:Npn \__nilcouv_check_unique_id:n #1
{
\seq_if_in:NnTF \g__nilcouv_scale_to_max_style_figure_ids_seq {#1}
{ \msg_error:nnn { nilcouv } { duplicate-figure-id } {#1} }
{ \seq_gput_right:Nn \g__nilcouv_scale_to_max_style_figure_ids_seq {#1} }
}
% Automatic generation of figure ids (the pattern is defined here)
\cs_new:Npn \__nilcouv_autogenerated_id:n #1 { nilcouv~autogenerated~id~#1 }
\cs_generate_variant:Nn \__nilcouv_autogenerated_id:n { V }
\cs_new_protected:Npn \__nilcouv_autoscale:nnn #1#2#3
{ \tikzset { scale~to~max~size={#1}{#2}{#3} } }
\cs_generate_variant:Nn \__nilcouv_autoscale:nnn { x }
\cs_new_protected:Npn \__nilcouv_autoscale_autoid:nn #1#2
{
% Increment the counter
\int_gincr:N \g_nilcouv_last_autogenerated_figure_nb_int
% Call the 'autoscale' style with the new id
\__nilcouv_autoscale:xnn
{ \__nilcouv_autogenerated_id:V
\g_nilcouv_last_autogenerated_figure_nb_int
}
{#1}
{#2}
}
% Set up aliases using LaTeX2e naming style
\cs_new_eq:NN \nilcouv@check@unique@id \__nilcouv_check_unique_id:n
\cs_new_eq:NN \nilcouv@autoscale@autoid \__nilcouv_autoscale_autoid:nn
\ExplSyntaxOff
% Autoscaling technique that doesn't affect font sizes in TikZ pictures.
% (based on code from marmot: <https://tex.stackexchange.com/a/497749/73317>)
%
% #1: unique per-picture id allowing several pictures to use this mechanism
% in a given document (it should contain no control sequence token nor
% active character)
% #2: target width
% #3: target height
\newcommand*{\nilcouv@ExportBB}[3]{%
\path let
\p1=($(current bounding box.north east)-(current bounding box.south west)$),
\n1={#2/\x1},\n2={#3/\y1}
in \pgfextra{\pgfmathsetmacro{\nilcouv@figscale}{min(\n1,\n2)}%
\expandafter\xdef\csname nilcouv@auto@figscale@#1\endcsname{%
\nilcouv@figscale}};
\immediate\write\@mainaux{%
\string\expandafter
\gdef\string\csname\space nilcouv@auto@figscale@#1\string\endcsname{%
\csname nilcouv@auto@figscale@#1\endcsname}}%
}
\tikzset{
% Arguments: figure identifier, target width, target height
scale to max size/.style n args={3}{
execute at end picture={\nilcouv@ExportBB{#1}{#2}{#3}},
/utils/exec={\nilcouv@check@unique@id{#1}%
\ifcsname nilcouv@auto@figscale@#1\endcsname
\wlog{Found autoscale value for picture '#1'}%
\else
\typeout{Automatically-scaled pictures: please recompile
for picture '#1'.}%
\expandafter\gdef
\csname nilcouv@auto@figscale@#1\endcsname{1}%
\fi},
scale=\csname nilcouv@auto@figscale@#1\endcsname,
},
% Same style except the id is automatically generated using a counter
autoscale autoid/.style 2 args={%
/utils/exec={\nilcouv@autoscale@autoid{#1}{#2}}},
}
% End of the code based on <https://tex.stackexchange.com/a/497749/73317>
\makeatother
\begin{document}
\noindent
\textcolor{orange}{\rule{15cm}{0.4pt}}\\ % print a 15cm long rule for comparison
\begin{tikzpicture}[
font={\sansmath\sffamily\Large}, line width=0.4mm, line cap=round,
line join=round, >=latex, x=1.92cm, y=1.92cm,
autoscale autoid={15cm}{\maxdimen} % <---------- this
]
\draw[->] (-5-.4,0) -- (2+.4,0);
\foreach \x in {-5,...,2} {
\draw[shift={(\x,0)},color=black] (0pt,2pt) -- (0pt,-2pt);
}
\foreach \x in {-1,1} {
\draw[shift={(\x,0)},color=black] (0,0) node[inner sep=5pt, above] {$\x$};
}
\draw[fill=orange]
(-2.9,0) coordinate (O) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$E$}
(-5,0) coordinate (A) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$F$}
(1.4,0) coordinate (B) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$G$}
(-2,0) coordinate (C) node[circle, draw=black, fill=orange, scale=0.4] {} node[inner sep=7pt, anchor=north, scale= 1] {$H$};
\end{tikzpicture}
\end{document}