
我有以下程式碼來創建必須在其上繪製三角函數(以度為單位)的軸。它將被列印出來並用作數學試卷的一部分。但是,x
軸的步長必須為 30 度。
如何修改下面的程式碼,將x-axis
, 上的每個標籤乘以 30?即標籤必須是0; 30; 60; 90; 120; ...
等
\documentclass[paper=a4, fontsize=11pt]{scrartcl}
\usepackage{tikz}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{gensymb}
\begin{document}
\begin{tikzpicture}
\draw[step = 0.5 cm, gray, very thin] (-1, -4) grid ( 13, 4);
\draw[thick, ->] (-1,0) -- (13,0) node[anchor = north west] {$x$};
\draw[thick, ->] (0,-4) -- (0,4) node[anchor = south east] {$y$};
\foreach \x in {1,...,13}
\draw (\x cm, 1pt) -- (\x cm, -1pt) node[anchor = north] {$\x \degree$};
\foreach \y in {-3,..., 3}
\draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor = east] {$\y$};
\end{tikzpicture}
\end{document}
答案1
我稍微更改了您的\foreach
聲明以包含[evaluate=\x as \degree using int(\x*30)]
並$\degree^\circ$
列印學位。您可以透過更改數字來更改步驟30
。
輸出
程式碼
\documentclass[margin=10pt]{standalone}
\usepackage{amsmath,amsfonts,amsthm}
\usepackage{gensymb}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[step = 0.5 cm, gray, very thin] (-1, -4) grid ( 13, 4);
\draw[thick, ->] (-1,0) -- (13,0) node[anchor = north west] {$x$};
\draw[thick, ->] (0,-4) -- (0,4) node[anchor = south east] {$y$};
\foreach \x [evaluate=\x as \degree using int(\x*30)] in {1,...,12}{
\draw (\x cm, 1pt) -- (\x cm, -1pt) node[anchor = north] {$\degree^\circ$};
}
\foreach \y in {-3,-2,-1,1,2,3}
\draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor = east] {$\y$};
\end{tikzpicture}
\end{document}
答案2
您可以使用\pgfmathparse
乘法。既然你得到了小數,你可能int
還需要像
node[anchor = north] {\pgfmathparse{int(30*\x)}$\SI{\pgfmathresult}{\degree}$};
請注意,我已經使用了siunitx
學位。
\documentclass{article}
\usepackage{tikz}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\draw[step = 0.5 cm, gray, very thin] (-1, -4) grid ( 13, 4);
\draw[thick, ->] (-1,0) -- (13,0) node[anchor = south west] {$x$};
\draw[thick, ->] (0,-4) -- (0,4) node[anchor = south west] {$y$};
\foreach \x in {1,...,13}
\draw (\x cm, 1pt) -- (\x cm, -1pt) node[anchor = north] {\pgfmathparse{int(30*\x)}$\SI{\pgfmathresult}{\degree}$};
\foreach \y in {-3,-2,-1,1,2, 3}
\draw (1pt, \y cm) -- (-1pt, \y cm) node[anchor = east] {$\y$};
\end{tikzpicture}
\end{document}
答案3
MetaPost 可能會對此感興趣。主要是為 x 軸選擇相關比例的問題,以及允許在該軸的標籤內循環的問題,這要歸功於\mplibtextextlabel{enable}
序言中的說明。這使得每個命令的字串參數label
都由 (Lua)LaTeX 透過textext
巨集進行排版,比通常的標誌靈活得多btex … etex
。
\documentclass[border=2mm]{standalone}
\usepackage{siunitx}
\usepackage{luamplib}
\mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
numeric u, v, xmin, xmax, xstep, ymin, ymax, ystep;
u = cm/30; xmin = -30; xmax = 390; xstep = 15;
v = cm; ymax = 4 = -ymin; ystep = .5;
beginfig(1);
% Grid
drawoptions(withcolor .8white);
for i = ceiling(xmin/xstep) upto floor(xmax/xstep):
draw ((i*xstep, ymin) -- (i*xstep, ymax)) xscaled u yscaled v;
endfor
for j = ceiling(ymin/ystep) upto floor(ymax/ystep):
draw ((xmin, j*ystep) -- (xmax, j*ystep)) xscaled u yscaled v;
endfor
drawoptions(); labeloffset := 5bp;
% x-axis marks and labels
for i = 2xstep step 2xstep until xmax-2xstep:
if i<>0:
label.bot("$" & decimal i & "\si\degree$", (i*u, 0));
draw (i*u, -2bp) -- (i*u, 2bp);
fi
endfor
% y-axis marks and labels
for j = ymin+1 step 2ystep until ymax-1:
if j<>0:
draw(-2bp, j*v) -- (2bp, j*v);
label.lft("$" & decimal j & "$", (0, j*v));
fi
endfor
% Axes and other labels
drawarrow (xmin*u, 0) -- (xmax*u, 0);
drawarrow (0, ymin*v) -- (0, ymax*v);
labeloffset := 3bp; label.llft("$O$", origin);
label.bot("$x$", (xmax*u, 0)); label.lft("$y$", (0, ymax*v));
endfig;
\end{mplibcode}
\end{document}
用LuaLaTeX排版。輸出:
答案4
PSTricks 解決方案:
\documentclass{article}
\usepackage[margin = 3cm]{geometry}
\usepackage{pst-plot}
\usepackage{siunitx}
\usepackage{xfp}
\makeatletter
\def\pst@@@hlabel#1{\ang{\fpeval{30*#1}}}
\makeatother
\begin{document}
\begin{pspicture*}(-1,-4)(13.36,4.4)
\psgrid[subgriddiv = 2, gridcolor = lightgray](-1,-4)(12.9,3.9)
\psaxes{->}(0,0)(-0.99,-3.99)(13,4)[$x$,0][$y$,90]
\end{pspicture*}
\end{document}