尽管已经阅读了 4-5 个相关问题的答案,但我仍然不明白为什么我的代码无法编译。代码如下:
\documentclass[11pt,twoside]{book}
\usepackage[paperheight=24cm, paperwidth=35cm, margin=0pt, %
voffset=-50cm, hoffset=-1.4cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\pagestyle{empty}
\begin{figure}[!h]
\centering\begin{tikzpicture}[xscale=1,yscale=1]
\draw[very thick,yellow,samples=500,domain=-6*pi:6*pi]
plot (\x, {cos((3*\x r)+pi/2)+\x*sin(3*\x r)});
\draw[very thick,orange,samples=500,domain=-6*pi:6*pi]
plot (\x, {-0.25*(pow(\x,2)*cos(\x r)-\x*sin(\x r))});
\draw[very thick,green,samples=500,domain=-6*pi:6*pi]
plot (\x, {cos(\x r)+\x});
\draw[very thick,magenta,samples=500,domain=-6*pi:6*pi]%% error!
plot (\x, {0.5*(pow(10,-11)*exp(-3*\x)-cos(\x r)+3*sin(\x r))});%% error!
\draw[very thick,blue,samples=500,domain=-6*pi:6*pi]%% error!
plot (\x, {pow(10,-15)*exp(3*\x)});%% error!
\end{tikzpicture}
\end{figure}
\end{document}
正如我在评论中展示的那样,五张图中最新的两张让我抓狂。请注意大量图voffset
。这有关系吗?显然我尝试了几种局部修改(例如辐射点等)。这只是一次测试,但现在我真的很好奇。
答案1
Percusse 已经对这个问题做出了相应的回答。(顺便说一句,我认为他的回答应该被标记为“已接受”)。不过,我冒昧地提出了一个 MetaPost 解决方案。
直到最近,这种函数绘制还无法用 MetaPost 实现,因为它仅基于非常有限的定点数值。但从 1.8 版开始,用户可以通过将内部变量设置为 随意切换到浮点数值numbersystem
。double
它仍然有点粗糙(例如,默认单位尚未调整),但它功能齐全,我忍不住要用它来解决这个问题。以下程序使用 LuaLaTeX 及其luamplib
包作为 MetaPost 的非常方便的接口。它调用 MetaPost 的 Metafun 格式,该格式定义了必要的辅助函数(cos、sin、exp……)
\documentclass[11pt]{standalone}
\usepackage{unicode-math}
\usepackage{luamplib}
\mplibsetformat{metafun}
\mplibtextextlabel{enable}
\mplibnumbersystem{double}
\begin{document}
\begin{mplibcode}
input mpcolornames;
% pi, cm (and mm) as accurate as possible
%(defaults settings are too inaccurate: pi = 3.14159265, cm = 28.34645)
pi:= 3.141592653589793;
cm := 3600/127; mm := 360/127;
% Unit lengths
u = .5cm; v = mm;
% Graphs boundaries
xmin = -6pi; xmax = -xmin; xsep = (xmax - xmin)/1000; ymin = -80; ymax = 100;
% Axes settings
Xmin = -20; Xmax = -Xmin; Ymin = -85; Ymax = 110;
% Macro building the graph of a given function f
vardef graph_of_function (suffix f) (expr xmin, xmax, xsep) =
for x = xmin step xsep until xmax: (x, f(x)) .. endfor (xmax, f(xmax))
enddef ;
% Functions to be graphed
vardef e(expr x) = cos(pi/2 + 3x) + x*sin 3x enddef;
vardef f(expr x) = -.25(x**2)*cos x - x*sin x enddef;
vardef g(expr x) = x + cos x enddef;
vardef h(expr x) = .5(-cos x + 3sin x + 1e-11exp -3x) enddef;
vardef i(expr x) = 1e-15exp 3x enddef;
%
beginfig(0);
% Drawing of the given functions
pickup pencircle scaled 1.25bp;
draw (graph_of_function(e)(xmin, xmax, xsep)) xyscaled (u, v) withcolor yellow;
draw (graph_of_function(f)(xmin, xmax, xsep)) xyscaled (u, v) withcolor Orange;
draw (graph_of_function(g)(xmin, xmax, xsep)) xyscaled (u, v) withcolor green;
draw (graph_of_function(h)(xmin, xmax, xsep)) xyscaled (u, v) withcolor magenta;
draw (graph_of_function(i)(xmin, xmax, xsep)) xyscaled (u, v) withcolor blue;
% Clipping
clip currentpicture to
((xmin, ymin) -- (xmax, ymin) -- (xmax, ymax) -- (xmin, ymax) -- cycle)
xyscaled (u, v);
% Axes and labels
pickup pencircle scaled .5bp;
drawarrow (Xmin*u, 0) -- (Xmax*u, 0); drawarrow (0, Ymin*v) -- (0, Ymax*v);
label.llft("$O$", origin);
label.lft("$y$", (0, Ymax*v)); label.bot("$x$", (Xmax*u, 0));
% Marking…
labeloffset := 6bp;
% … on the horizontal axis
draw (u*pi, -3bp) -- (u*pi, 3bp); draw (-u*pi, -3bp) -- (-u*pi, 3bp);
label.bot("$\pi$", (pi*u, 0)); label.bot("$-\pi$", (-pi*u, 0));
for i = 2 upto 6:
draw (i*pi*u, -3bp)-- (i*pi*u, 3bp);
label.bot("$" & decimal i & "\pi$", (i*pi*u, 0));
draw (-i*pi*u, -3bp)-- (-i*pi*u, 3bp);
label.bot("$" & decimal -i & "\pi$", (-i*pi*u, 0));
endfor;
% … on the vertical axis
for i = 20 step 20 until 80:
label.lft("$" & decimal i & "$", (0, i*v));
label.lft("$" & decimal -i & "$", (0, -i*v));
draw (-3bp, i*v) -- (3bp, i*v);
draw (-3bp, -i*v) -- (3bp, -i*v);
endfor;
label.lft("$100$", (0, 100v)); draw (-3bp, 100v) -- (3bp, 100v);
% Preventing possible cropping of labels at the figure boundaries
setbounds currentpicture to boundingbox currentpicture enlarged .5cm;
endfig;
\end{mplibcode}
\end{document}
答案2
只需简单使用pgfplots
。TikZ 在这种情况下无法处理这种精度。事实上,我只是复制粘贴并替换了命令。还调整了 y 域以使图可见。
\documentclass[11pt,twoside]{book}
\usepackage{pgfplots}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[samples=500,domain=-6*pi:6*pi,restrict y to domain =-20:100]
\addplot[very thick,yellow ]plot (\x, {cos((3*\x r)+pi/2)+\x*sin(3*\x r)});
\addplot[very thick,orange ]plot (\x, {-0.25*(pow(\x,2)*cos(\x r)-\x*sin(\x r))});
\addplot[very thick,green ]plot (\x, {cos(\x r)+\x});
\addplot[very thick,magenta] plot (\x, {0.5*(pow(10,-11)*exp(-3*\x)-cos(\x r)+3*sin(\x r))});
\addplot[very thick,blue ] plot (\x, {pow(10,-15)*exp(3*\x)});
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案3
我真的忘了发布我最终是如何处理它的:在必要时修改函数域(参见%% error!
问题代码)。
\documentclass[11pt,twoside]{book}
\usepackage[paperheight=24cm, paperwidth=35cm, margin=0pt, %
voffset=-140cm, hoffset=-1.4cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\pagestyle{empty}
\begin{figure}[!h]
\centering\begin{tikzpicture}%[xscale=1,yscale=1]
\draw[very thick,yellow,samples=500,domain=-6*pi:6*pi]
plot (\x, {cos((3*\x r)+pi/2)+\x*sin(3*\x r)});
\draw[very thick,orange,samples=500,domain=-6*pi:6*pi]
plot (\x, {-0.25*(pow(\x,2)*cos(\x r)-\x*sin(\x r))});
\draw[very thick,green,samples=500,domain=-6*pi:6*pi]
plot (\x, {cos(\x r)+\x});
\draw[very thick,magenta,samples=500,domain=-pi:6*pi]
plot (\x, {0.5*(pow(10,-2)*exp(-3*\x)-cos(\x r)+3*sin(\x r))});%
\draw[very thick,blue,samples=500,domain=-6*pi:.8*pi]
plot (\x+1, {exp(2*\x)});%
\end{tikzpicture}
\end{figure}
\end{document}
standalone
顺便说一句,我认为使用documentclass 和 TikZ 命令可以做得更好clip
。下次我必须为整本书的封面制作一个情节图时,我会发布更新!