我正在尝试绘制 14 米长的厘米级尺子,每次一米。我希望用胶纸打印出来以测量一些东西,但这并没有什么用。
到目前为止,我已经运行了此代码 (MWE),它产生了一米长的尺子。我附上了一张长约 22 厘米的图像。
\documentclass[12pt]{standalone}
\renewcommand*\familydefault{\sfdefault}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% \foreach \i in {0,100,...,130}
% Draw Minor tick lines
\foreach \x in {99,97,...,1}
\draw [color=red, line width=6mm](\x cm,0cm) -- (\x cm ,1.5cm)
node[color=red, rotate=90, anchor=west] {\x};
\foreach \x in {100,98,...,0}
\draw [color=black, line width=6mm](\x cm,0cm) -- (\x cm ,1.5cm)
node[color=black, rotate=90, anchor=west] {\x};
Draw Major tick lines
\foreach \x in {100,90,...,0}
\draw [color=black,line width=6mm] (\x cm,4cm) -- (\x cm ,0cm)
node[anchor=north] {\Huge \textbf{\x}};
\foreach \x in {95,85,...,5}
\draw [color=red, line width=6mm] (\x cm,4cm) -- (\x cm ,0cm)
node[anchor=north] {\Huge \textbf{\x}};
% Draw main axis
% \draw [line width=6mm](0cm, \i cm) -- coordinate (x axis mid) (100cm,\i cm);
\draw [line width=6mm](0cm, 0 cm) -- coordinate (x axis mid) (100cm, 0 cm);
\end{tikzpicture}
\end{document}
我面临一些问题。
- 我需要数字从右向左增加。目前,尽管我指定了降序
\foreach
,但它们还是从左向右增加。我该如何实现这一点? - 标尺的末端没有斜接。我尝试使用命令
join=miter
中的选项\draw
,但没有效果。 - 最后,我如何使用
\foreach
命令(如果有的话)将单个标尺变成垂直堆叠的十四条标尺,并采用正确的编号。我设法完成了标尺轴的重复(如您在代码中看到的注释),但当我尝试对刻度线和节点执行相同操作时,不断出现可怕的错误。
还有一些其他的事情我可以忍受,比如用主刻度线覆盖重叠的次要刻度线数字,但是知道如何指定正确的条件来防止这种情况就好了。
答案1
循环中的顺序
\foreach
与绘制顺序无关。如果指定(\x cm,0cm)
,则坐标位于(\x cm,0cm)
(在画布系统中)。
我建议简单地反转XTikZ 坐标系的方向:x=-1cm
。这意味着如果你没有为X坐标的变量,TikZ 会将该数字与 相乘-1cm
。当然,你也可以简单地使用,-\x cm
但这个解决方案更灵活一些。line cap=rect
使同一线段的末端butt
(默认)仅延长一半的线宽。我们只需将一米放在另一个从(第一米)到(第十四米)
\foreach
的循环中即可。第一步是,对于米来说大约,然后最多。如果您想要不同的偏移,即米之间的距离,您可以使用 PGF 数学:。0
13
yshift
0
00cm
10cm
130cm
yshift=\i*7cm
代码
\documentclass[tikz,convert=false,12pt]{standalone}
\renewcommand*\familydefault{\sfdefault}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}
\begin{document}
\begin{tikzpicture}[x=-1cm,line cap=rect]
\foreach \i in {0,...,13}{
\tikzset{yshift=\i0cm}
\foreach \x in {1,3,...,99}
\draw [color=red, line width=6mm](\x,0cm) -- (\x,1.5cm)
node[color=red, rotate=90, anchor=west] {\pgfmathprint{int(\x+\i*100)}};
\foreach \x in {0,2,...,98}
\draw [color=black, line width=6mm](\x,0cm) -- (\x,1.5cm)
node[color=black, rotate=90, anchor=west] {\pgfmathprint{int(\x+\i*100)}};
\foreach \x in {0,10,...,100}
\draw [color=black,line width=6mm] (\x,4cm) -- (\x,0cm)
node[anchor=north] {\Huge \textbf{{\pgfmathprint{int(\x+\i*100)}}}};
\foreach \x in {5,15,...,95}
\draw [color=red, line width=6mm] (\x,4cm) -- (\x,0cm)
node[anchor=north] {\Huge \textbf{\pgfmathprint{int(\x+\i*100)}}};
\draw [line width=6mm](0, 0cm) -- coordinate (x axis mid) (100,0cm);
}
\end{tikzpicture}
\end{document}
输出
答案2
感谢这篇帖子和讨论!我遇到了类似的问题,它对入门很有帮助。但后来又有一些额外的功能……这就是我想分享我的结果的原因。
基本上,这是两条双线标尺,用于类似 xy 绘图仪的布置,带有标记坐标。
干得好:
\documentclass[tikz,convert=false,12pt,a3paper]{standalone}
\renewcommand*\familydefault{\sfdefault}
\renewcommand\familydefault{\sfdefault}
\usepackage[T1]{fontenc}
\begin{document}
\begin{tikzpicture}[x=-1cm,line cap=rect]
\def\zeile{36}
\def\tickl{1cm}
\def\tickxl{3cm}
\def\tickm{.6cm}
\def\ticks{.4cm}
\def\spaceb{1cm}
\def\spacet{4cm}
% draw labels below ruler
%% x labels
\foreach \p [count=\mycount from 1] in {20,30,31,42,53,54.3} {
\pgfmathparse{ifthenelse(\p > \zeile,{\p-\zeile+1},{\p}}
\global\let\pos=\pgfmathresult;
\pgfmathparse{ifthenelse(\p > \zeile,4,0}
\global\let\shift=\pgfmathresult;
\draw [color=red,line width=3pt](\pos,\shift) -- ++ (0,\tickxl)
node[color=red, rotate=90, anchor=west,above left] {\large {$\mathsf{ x_\mycount}$}};
}
% y labels
\foreach \p [count=\mycount from 1] in {15,20,31,35,36.5,53,54.3}{
\pgfmathparse{ifthenelse(\p > \zeile,{\p-\zeile+1},{\p}}
\global\let\pos=\pgfmathresult;
\pgfmathparse{ifthenelse(\p > \zeile,3*4,2*4}
% TODO: how to use variable \spacet without unit cm?
\global\let\shift=\pgfmathresult;
\draw [color=blue,line width=3pt](\pos,\shift) -- ++ (0,\tickxl)
node[color=blue, rotate=90, anchor=west,above left] {\large {$\mathsf{ y_\mycount}$}};
}
\draw [dashed](0, -1cm) -- ++ (\zeile,0cm);
\foreach \j in {0,2,4}{ % one x, one y, one blank
\foreach \i in {0,1}{ % two lines per dim required
\tikzset{yshift=(\i+\j)*\spacet}
% mm ticks without label
\foreach \x in {0,0.1,0.2,...,\zeile}
\draw [color=black, line width=1pt](\x,0cm) -- (\x,\ticks);
\foreach \x in {0,0.5,1,...,\zeile}
\draw [color=black, line width=1pt](\x,0cm) -- (\x,\tickm);
% cm ticks with label
\foreach \x in {1,6,...,\zeile}
\draw [color=black, line width=2pt](\x,0cm) -- (\x,\tickm) node[color=black, rotate=90, anchor=west] {\pgfmathprint{int(\x+\i*\zeile-1*\i)}};
\foreach \x in {2,7,...,\zeile}
\draw [color=black, line width=2pt](\x,0cm) -- (\x,\tickm) node[color=black, rotate=90, anchor=west] {\pgfmathprint{int(\x+\i*\zeile-1*\i)}};
\foreach \x in {3,8,...,\zeile}
\draw [color=black, line width=2pt](\x,0cm) -- (\x,\tickm) node[color=black, rotate=90, anchor=west] {\pgfmathprint{int(\x+\i*\zeile-1*\i)}};
\foreach \x in {4,9,...,\zeile}
\draw [color=black, line width=2pt](\x,0cm) -- (\x,\tickm) node[color=black, rotate=90, anchor=west] {\pgfmathprint{int(\x+\i*\zeile-1*\i)}};
% each 5cm large tick
\foreach \x in {0,5,...,\zeile}
\draw [color=black,line width=3pt](\x,0cm) -- (\x,\tickl)
node[color=black, rotate=90, anchor=west] {\large \textbf{{\pgfmathprint{int(\x+\i*\zeile-1*\i)}}}};
% axis line & cut line
\draw [line width=1mm](0, 0cm) -- (\zeile,0cm);
\draw [dashed](0,3cm) -- ++ (\zeile,0cm);
}
}
\end{tikzpicture}
\end{document}