
저는 TikZ를 사용하는 방법을 배우고 있으며 보다 간결한 TikZ 코드를 작성하는 방법을 배우고 싶습니다. 나는 동일한 결과를 얻기 위해 다른 사람들보다 더 많은 라인을 사용한다고 생각합니다. 특히 모든 TikZ 문서에 사용할 수 있는 간결한 코드를 작성하는 기술이나 연습을 배우고 싶습니다.
예를 들어 다음 이미지를 고려해보세요.
… 아래 코드로 생성되었습니다(가독성을 위해 공백이 제공되었습니다). 이 코드를 실행하려면 TikZ 및 pgf를 업데이트해야 할 수도 있습니다. angles
및 quotes
라이브러리는 2014년 5월 기준으로 비교적 새로운 것이기 때문입니다.
\documentclass[tikz]{standalone}%
\usetikzlibrary{calc}
\usetikzlibrary{intersections}
\usetikzlibrary{angles}
\usetikzlibrary{quotes}
\begin{document}
\begin{tikzpicture}
\coordinate (A) at (0, 0);
\coordinate (B) at (3, 0);
\coordinate (C) at (3, 2);
\draw[name path = tri] (A) -- (B) -- (C) -- cycle pic["$\alpha$", -stealth,
draw, angle radius = 1cm, angle eccentricity = 1.25] {angle = B--A--C};
\path[name path = line] (0, 1.4) -- +(2.9, 0);
\path[name intersections = {of = line and tri, by = P1}];
\path (P1) -- ($(P1)!.25cm!-90:(A)$) coordinate (cylinder);
\path[name path = line2] (cylinder) -- +(1.05, 0);
\path[name intersections = {of = line2 and tri}];
\draw (cylinder) circle[radius = 0.25cm];
\draw[dashed, gray] (cylinder) -- (intersection-1);
\draw[stealth-stealth] (3.15, 2) -- ($(intersection-1) + (.15, 0)$);
\draw (3.1, 2) -- (3.2, 2);
\draw ($(intersection-1) + (.1, 0)$) -- +(.1, 0);
\draw (cylinder) -- +(45:.25);
\pgfmathsetmacro{\angle}{atan(2/3)};
\pgfmathsetmacro{\ppi}{\angle + 180}
\begin{scope}[rotate = \angle]
\clip ($(cylinder) + (0, .4)$) rectangle ($(cylinder) + (-.4, 0)$);
\draw[name path global = rotation] (cylinder) circle[radius = .395cm];
\end{scope}
\path[name path = line3] (cylinder) -- +(\ppi:.4);
\path[name intersections = {of = line3 and rotation, by = P2}];
\end{tikzpicture}
\end{document}
답변1
각도 화살표 없이 일부 단축. 하지만 어쨌든 라이브러리를 사용하면 가능합니다 angle
. 각도 변경이 가능합니다.
\documentclass[tikz]{standalone}%
\begin{document}
\begin{tikzpicture}
\def\myang{35}
\draw (0,0) coordinate(o) --+(\myang:1cm) +(1,0)arc (0:\myang:1cm)
node[right,midway]{$\alpha$}--(\myang:3cm) coordinate (a)
node[minimum size=5mm,circle,draw,pos=0.4,anchor={-90+\myang}] (b) {}--(o-|a)
(b.center)--(b.\myang) (b.210)++(210:1mm) arc (210:120:3.5mm);
\draw[dashed] (b.center) -- (a|-b);
\draw[>=latex,|<->|] ([xshift=2mm]a|-b) -- ([xshift=2mm]a);
\end{tikzpicture}
\end{document}
답변2
이것은 더 간결합니다. (그리고 대부분의 tikz 코드보다 가독성도 떨어집니다:-)
\documentclass[tikz]{standalone}\usetikzlibrary{calc,intersections,
angles,quotes}\def\c{\coordinate}\def\d{\draw}\def\p{\path}
\def\pp#1#2#3#4#5{\p[name path=#1](#2)--+(#3);\p[name intersections={of=#1
and #4,#5}];}\let\q\pgfmathsetmacro
\begin{document}\def\z#1#2#3#4#5#6#7#8{\begin{tikzpicture}
\c(A)at(0,0);\c(B)at(3,0);\c(C)at(3,2);\d[name path=#7](A#6(B#6(C#6cycle
pic["$\alpha$",-#3,draw,#1 #4=1cm,#1 eccen#7city=1.25]{#1=B--A--C};\pp
{#8}{0,1.4}{2.9,0}{#7}{by=P1}\p(P1#6($(P1)!.25cm!-90:(A)$)coordinate(#2);
\pp{#82}{#2}{1.05,0}{#7}{}\d(#2)circle[#4=0.25cm];\d[dashed,gray](#2#6(#5-1);
\d[#3-#3](3.15,2#6($(#5-1)+(.15,0)$);\d(3.1,2#6(3.2,2);\d($(#5-1)+(.1,0)$#6+
(.1,0);\d(#2#6+(45:.25);\q{\angle}{atan(2/3)};\q{\ppi}{\angle+180}\begin
{scope}[rotate=\angle]\clip($(#2)+(0,.4)$)rect#1($(#2)+(-.4,0)$);
\d[name path global=rotation](#2)circle[#4=.395cm];\end{scope}\pp{#83}{#2}
{\ppi:.4}{rotation}{by=P2}\end{tikzpicture}}\z{angle}{cylinder}{stealth}
{radius}{intersection}{)--}{tri}{line}\end{document}
답변3
애니메이션으로는 아직 너무 짧습니다.
\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-eucl}
\psset
{
CurveType=polygon,
PointName=none,
PointSymbol=none,
LabelSep=.5,
MarkAngleRadius=1,
}
\begin{document}
\multido{\n=3.14+.10}{50}{%
\begin{pspicture}(8,6)
\pstGeonode{A}(7,0){B}(7,6){C}
\pstMarkAngle[arrows=->]{B}{A}{C}{$\alpha$}
\pnode([offset=.5,nodesep=\n]{C}A){P}
\pscircle(P){.5}
\rput{(C)}(P){\psarc(P){.6}{90}{180}\psline(P)([nodesep=.5,angle={!\n\space 4 mul Pi add 2 div neg RadtoDeg}]P)}
\pcline[offset=.5]{|*-|*}(C)(C|P)
\pcline[linestyle=dashed,nodesepB=-.5](P)(C|P)
\end{pspicture}}
\end{document}