TeX - LaTeX Asked on November 1, 2021
I’m new to Tikz. Please, consider the following minimal example:
documentclass{article}
usepackage{tikz}
begin{document}
begin{tikzpicture}
draw[very thick] (-2,-2) rectangle (2,2);
draw (-2,-2) grid (2,2);
fill (1,1) circle [radius=3pt];
end{tikzpicture}
begin{tikzpicture}[x=0.5cm,y=0.5cm]
draw[very thick] (-2,-2) rectangle (2,2);
draw (-2,-2) grid (2,2);
fill (1,1) circle [radius=3pt];
end{tikzpicture}
end{document}
The problem I have is that in the second picture, the grid is not taking into account the unit lengths. What am I missing? What should I do to let the grid "know" that I’ve changed the units?
According to the pgfmanual, the grid is drawn with steps as configured through /tikz/step=h
which as far as the manual says is a number or dimension or coordinate
and has (no default, initially 1cm)
.
So if you want the grid to be not drawn with 1cm, you have to change the step
, as in (or locally, just for the grid drawing command):
documentclass{article}
usepackage{tikz}
begin{document}
begin{tikzpicture}
draw[very thick] (-2,-2) rectangle (2,2);
draw (-2,-2) grid (2,2);
fill (1,1) circle [radius=3pt];
end{tikzpicture}
begin{tikzpicture}[x=0.5cm,y=0.5cm,step=0.5cm]
draw[very thick] (-2,-2) rectangle (2,2);
draw (-2,-2) grid (2,2);
fill (1,1) circle [radius=3pt];
end{tikzpicture}
end{document}
Answered by TobiBS on November 1, 2021
Grid separation is controlled by xstep
and ystep
which store the value in tikz@grid@x
and tikz@grid@y
respectively. Their initial value are defined in tikz.code.tex
deftikz@grid@x{1cm}%
deftikz@grid@y{1cm}%
In your second example, you set x=0.5cm, y=0.5cm
, which means (-2, -2)
is parsed as (-1cm, -1cm)
and (2, 2)
is parsed as (1cm, 1cm)
. This is because when coordinates is passed to parse, they are first checked if the x and y coordinate is with unit by tikz@checkunit
. Then tikz can judge if it is with a unit by iftikz@isdimension
, if not the coordinate is set by pgfpointxy
, otherwise the coordinate is set by pgfpoint
. Actually there are four cases to handle, related code is
deftikz@@@parse@regular#1#2#3){%
pgfutil@in@,{#3}%
ifpgfutil@in@%
tikz@parse@splitxyz{#1}{#2}#3,%
else%
tikz@checkunit{#2}%
iftikz@isdimension%
tikz@checkunit{#3}%
iftikz@isdimension%
defpgfutil@next{#1{pgfpoint{#2}{#3}}}%
else%
defpgfutil@next{#1{pgfpointadd{pgfpoint{#2}{0pt}}{pgfpointxy{0}{#3}}}}%
fi%
else%
tikz@checkunit{#3}%
iftikz@isdimension%
defpgfutil@next{#1{pgfpointadd{pgfpoint{0pt}{#3}}{pgfpointxy{#2}{0}}}}%
else%
defpgfutil@next{#1{pgfpointxy{#2}{#3}}}%
fi%
fi%
fi%
pgfutil@next%
}%
Similar parse mechanism is also applied to xstep
and ystep
tikz@checkunit{tikz@grid@x}%
iftikz@isdimension%
pgf@process{pgfpoint{tikz@grid@x}{0pt}}%
else%
pgf@process{pgfpointxy{tikz@grid@x}{0}}%
fi%
pgf@xb=pgf@x%
pgf@yb=pgf@y%
tikz@checkunit{tikz@grid@y}%
iftikz@isdimension%
pgf@process{pgfpoint{0pt}{tikz@grid@y}}%
else%
pgf@process{pgfpointxy{0}{tikz@grid@y}}%
fi%
advancepgf@xb bypgf@x%
advancepgf@yb bypgf@y%
pgfpathgrid[stepx=pgf@xb,stepy=pgf@yb]%
{pgfqpoint{pgf@xa}{pgf@ya}}{pgfqpoint{tikz@lastx}{tikz@lasty}}%
In your example they are just 1cm
, so the same fig can be draw by
draw (-1cm, -1cm) grid (1cm, 1cm);
and fill (1, 1) circle [radius=3pt];
is equal to fill (0.5cm, 0.5cm) circle (3pt);
which is not on any grid point.
Answered by ZhiyuanLck on November 1, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP