레슨 9 / 10·4개 토픽
TikZ 그래픽
TikZ는 LaTeX 문서 안에서 벡터 그래픽을 직접 그릴 수 있는 강력한 패키지입니다. 다이어그램, 플로차트, 그래프, 수학적 도형 등을 프로그래밍 방식으로 정밀하게 작성할 수 있습니다.
기본 도형 그리기
latex
\usepackage{tikz}
\begin{tikzpicture}
% 선 그리기
\draw (0,0) -- (3,0); % 수평선
\draw (0,0) -- (3,2); % 대각선
\draw[thick, red] (0,0) -- (0,3); % 굵은 빨간 수직선
% 사각형
\draw (0,0) rectangle (3,2);
\draw[blue, fill=blue!20] (4,0) rectangle (7,2);
% 원
\draw (1.5,4) circle (1);
\fill[green!30] (5.5,4) circle (0.8);
% 타원
\draw (1.5,7) ellipse (1.5 and 0.8);
% 호 (arc)
\draw (5,7) arc (0:180:1); % 시작각:끝각:반지름
% 둥근 모서리 사각형
\draw[rounded corners=5pt] (0,9) rectangle (3,10.5);
\end{tikzpicture}노드와 텍스트
latex
\begin{tikzpicture}
% 기본 노드
\node at (0,0) {텍스트};
\node[draw] at (3,0) {테두리 있는 노드};
\node[draw, circle] at (6,0) {원형};
% 노드 스타일
\node[draw, rectangle, fill=yellow!30,
minimum width=2cm, minimum height=1cm]
at (0,-2) {스타일 적용};
% 이름 있는 노드 (연결에 사용)
\node[draw, circle] (A) at (0,-4) {A};
\node[draw, circle] (B) at (3,-4) {B};
\node[draw, circle] (C) at (6,-4) {C};
% 노드 간 연결
\draw[->] (A) -- (B);
\draw[->, thick] (B) -- (C);
\draw[->, dashed] (A) to[bend left=30] (C);
% 연결선 위에 라벨
\draw[->] (A) -- node[above] {5} (B);
\draw[->] (B) -- node[below] {3} (C);
\end{tikzpicture}플로차트 그리기
latex
\usetikzlibrary{shapes.geometric, arrows.meta, positioning}
\begin{tikzpicture}[
startstop/.style={rectangle, rounded corners, draw, fill=red!30,
minimum width=3cm, minimum height=1cm},
process/.style={rectangle, draw, fill=blue!20,
minimum width=3cm, minimum height=1cm},
decision/.style={diamond, draw, fill=green!20,
minimum width=2.5cm, minimum height=1cm, aspect=2},
arrow/.style={thick, -Stealth},
node distance=1.5cm
]
\node[startstop] (start) {시작};
\node[process, below of=start] (input) {데이터 입력};
\node[decision, below of=input] (decide) {유효한가?};
\node[process, below of=decide, yshift=-0.5cm] (process) {데이터 처리};
\node[process, right of=decide, xshift=3cm] (error) {오류 처리};
\node[startstop, below of=process] (stop) {종료};
\draw[arrow] (start) -- (input);
\draw[arrow] (input) -- (decide);
\draw[arrow] (decide) -- node[left] {예} (process);
\draw[arrow] (decide) -- node[above] {아니오} (error);
\draw[arrow] (error) |- (input);
\draw[arrow] (process) -- (stop);
\end{tikzpicture}좌표계와 고급 경로
latex
\begin{tikzpicture}
% 절대 좌표
\draw (0,0) -- (2,1) -- (4,0);
% 상대 좌표 (++ : 이전 점 기준)
\draw (0,-2) -- ++(2,1) -- ++(2,-1);
% 그리드 (격자)
\draw[step=0.5, gray!30, very thin] (0,-5) grid (6,-3);
\draw[thick, ->] (0,-5) -- (6.3,-5) node[right] {$x$};
\draw[thick, ->] (0,-5) -- (0,-2.7) node[above] {$y$};
% 함수 그래프 (plot)
\draw[domain=0:6, smooth, blue, thick]
plot (\x, {-4 + sin(\x * 60)});
% 데이터 포인트 표시
\foreach \x/\y in {1/-3.5, 2/-4.2, 3/-3.1, 4/-4.5, 5/-3.8} {
\fill[red] (\x, \y) circle (2pt);
}
% 화살표 스타일
\draw[->, >=latex] (0,-7) -- (2,-7); % LaTeX 화살표
\draw[->, >=stealth] (3,-7) -- (5,-7); % Stealth 화살표
\draw[<->, >=stealth] (0,-8) -- (5,-8); % 양방향
% 패턴 채우기
\fill[pattern=north east lines] (0,-10) rectangle (2,-9);
\fill[pattern=dots] (3,-10) rectangle (5,-9);
\end{tikzpicture}💡
TikZ는 \usetikzlibrary{}로 추가 기능을 불러올 수 있습니다. positioning(상대 배치), arrows.meta(화살표), shapes.geometric(도형), calc(좌표 계산) 등이 자주 사용됩니다. 복잡한 그림은 \tikzset으로 스타일을 미리 정의하면 코드가 깔끔해집니다.