LaTeX

TikZ로 array 그리기

알 수 없는 사용자 2009. 4. 18. 16:54
Array 그리기 노가다를 좀 덜어보...자?

\begin{tikzpicture}

% size of each node

\def\sz{8mm}

% node style definition

\tikzstyle{block} = [

draw, fill=black!10, rectangle,

minimum height=\sz, minimum width=\sz ];

\tikzstyle{plain} = [draw=none,fill=none];

% array element definition

\def\arr{6,0,2,0,1,3,4,6,1,3,2};

%\def\x{0}; % x pos of arr

%\def\y{0}; % y pos of arr

\newcounter{ind};

\setcounter{ind}{0};

\node[plain] { A };

\foreach \item in \arr

{

\addtocounter{ind}{1};

\node[block] at (\theind*\sz,0) { \item };

\node[plain] at (\theind*\sz,0.7) { \theind };

}

\end{tikzpicture}



\def\arr{} 부분만 바뀌면 저런 그림이 생성된다.

이번에는 조금 다른거...

\newcounter{ind};

\newcounter{indx};

\begin{tikzpicture}

% size of each node

\def\sz{8mm}

% node style definition

\tikzstyle{block} = [

draw, fill=black!10, rectangle,

minimum height=\sz, minimum width=\sz ];

\tikzstyle{dblock} = [

draw, fill=black!40, rectangle,

minimum height=\sz, minimum width=\sz ];

\tikzstyle{plain} = [draw=none,fill=none];

% array element definition

%\def\arra{6,0,2,0,1,3,4,6,1,3,2};

%\def\y{2}; % y pos of arr

%\setcounter{ind}{0};

%\node[plain] at (0,\y) { A };

%\foreach \item in \arra

%{

% \addtocounter{ind}{1};

% \node[block] at (\theind*\sz,\y+0) { \item };

% \node[plain] at (\theind*\sz,\y+0.7) { \theind };

%}

% array element definition

\def\arra{6,0,2,0,1,3,4,6,1};

\def\arrc{2,4,5,7,9,9,11};

\def\arrb {1,2,3,4,5,2,7,3,9,10,11};

\def\black{1,2,3,4,5,7,9,10,11};

\def\y{0}; % y pos of arr

\setcounter{ind}{0};

\node[plain] at (0,\y) { B };

\foreach \item in \arrb

{

\addtocounter{ind}{1};

\node[block] at (\theind*\sz,\y+0) { \item };

\node[plain] at (\theind*\sz,\y+0.7) { \theind };

}

\def\y{-2}; % y pos of arr

\setcounter{ind}{0};

\setcounter{indx}{-1};

\node[plain] at (0,\y) { C };

\foreach \item in \arrc

{

\addtocounter{ind}{1};

\addtocounter{indx}{1};

\node[block] at (\theind*\sz,\y+0) { \item };

\node[plain] at (\theind*\sz,\y+0.7) { \theindx };

}

% draw darker box over hidden node.

\foreach \item in \black

{

\node[dblock] at (\item*\sz,0) {};

}

\end{tikzpicture}


미완성이다가 점점 차가는 array를 그려주는 코드... -_-;;;