我正在尝试编写一个函数来简化图形的绘制。我\drawcurve
按如下所示调用此函数。它的目的是在其参数中用坐标表示的两条线之间创建一条曲线。它看起来像这样:
\drawcurve{<line1_co1>}{<line1_co2>}{<line2_co1>}{<line2_co2>}{<curve settings>}
然而,我遇到了一个问题,由于我对坐标 v1 和 v2 的定义,tikz 库还在图形上为这些未绘制的坐标保留了空间,导致我的图形不平衡。到目前为止,我还无法弄清楚如何让 tikz 不为这些坐标保留空间或完全避免使用坐标定义。我该如何做到这一点?
我制作的图形的代码也包括在内。在这种情况下,问题似乎并不是非常严重,但由于我想\drawcurve
在论文中经常使用该命令,因此它需要在每种情况下都能工作。
我希望我的问题很清楚。
谢谢!
\newcommand{\drawcurve}[5]{
\draw
#1 coordinate (start_far)
#2 coordinate (start_close)
($(start_close) - (start_far)$) coordinate (v1)
#3 coordinate (end_close)
#4 coordinate (end_far)
($(end_close) - (end_far)$) coordinate (v2)
;
\draw
let \p1=(v1), \n1={atan2(\y1,\x1)} in
let \p2=(v2), \n2={atan2(\y2,\x2)} in
[#5] (start_close) to[out=\n1,in=\n2] (end_close)
;
}
\documentclass[a4paper, 10pt]{article}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{fit}
\begin{document}
\begin{figure}[h!]
\centering
\begin{tikzpicture}
% AXES
\draw[->] (0, 0) -- (0, 5) node[above] {$A[\qty{}{\decibel}]$};
\draw[->] (0, 0) -- (8, 0) node[right] {$f [\qty{}{\hertz}]$};
% DRAW DOTTED LINES
\draw[dotted, thick] (8,3) -- (0, 3);
\draw[dotted, thick] (4,0) -- (4, 4);
% DRAW CURVES
\draw[color=blue, ultra thick] (0,3.5) -- (3.5,3.5);
\drawcurve{(0,3.5)}{(3.5,3.5)}{(5,2.5)}{(6.5,1)}{color=blue, ultra thick}
\draw[color=blue, ultra thick] (5,2.5) -- (6.5,1);
\draw[color=red, ultra thick] (0,2.5) -- (3.5,2.5);
\drawcurve{(0,2.5)}{(3.5,2.5)}{(5,3.5)}{(5.5,4)}{color=red, ultra thick}
\draw[color=red, ultra thick] (5,3.5) -- (5.5,4);
\drawcurve{(5,3.5)}{(5.5,4)}{(6.5,4)}{(8,2.5)}{color=red, ultra thick}
\draw[color=red, ultra thick] (6.5,4) -- (8,2.5);
\draw[color=green, ultra thick] (0,3) -- (5.5,3);
\drawcurve{(0,3)}{(5.5,3)}{(7,2)}{(8,1)}{color=green, ultra thick}
\draw[color=green, ultra thick] (7,2) -- (8,1);
\end{tikzpicture}
\caption{}
\label{fig:myfig}
\end{figure}
\end{document}
答案1
您可以在自定义宏中定义坐标的路径中添加该选项overlay
。这样,此路径就不会添加到图片的边界框中:
\documentclass{standalone}
\usepackage{siunitx}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\drawcurve}[5]{
\path[overlay]
#1 coordinate (start_far)
#2 coordinate (start_close)
($(start_close) - (start_far)$) coordinate (v1)
#3 coordinate (end_close)
#4 coordinate (end_far)
($(end_close) - (end_far)$) coordinate (v2)
;
\draw
let \p1=(v1), \n1={atan2(\y1,\x1)} in
let \p2=(v2), \n2={atan2(\y2,\x2)} in
[#5] (start_close) to[out=\n1,in=\n2] (end_close)
;
}
\begin{document}
\begin{tikzpicture}
% AXES
\draw[->] (0, 0) -- (0, 5) node[above] {$A[\qty{}{\decibel}]$};
\draw[->] (0, 0) -- (8, 0) node[right] {$f [\qty{}{\hertz}]$};
% DRAW DOTTED LINES
\draw[dotted, thick] (8,3) -- (0, 3);
\draw[dotted, thick] (4,0) -- (4, 4);
% DRAW CURVES
\draw[color=blue, ultra thick] (0,3.5) -- (3.5,3.5);
\drawcurve{(0,3.5)}{(3.5,3.5)}{(5,2.5)}{(6.5,1)}{color=blue, ultra thick}
\draw[color=blue, ultra thick] (5,2.5) -- (6.5,1);
\draw[color=red, ultra thick] (0,2.5) -- (3.5,2.5);
\drawcurve{(0,2.5)}{(3.5,2.5)}{(5,3.5)}{(5.5,4)}{color=red, ultra thick}
\draw[color=red, ultra thick] (5,3.5) -- (5.5,4);
\drawcurve{(5,3.5)}{(5.5,4)}{(6.5,4)}{(8,2.5)}{color=red, ultra thick}
\draw[color=red, ultra thick] (6.5,4) -- (8,2.5);
\draw[color=green, ultra thick] (0,3) -- (5.5,3);
\drawcurve{(0,3)}{(5.5,3)}{(7,2)}{(8,1)}{color=green, ultra thick}
\draw[color=green, ultra thick] (7,2) -- (8,1);
\end{tikzpicture}
\end{document}