好的。我试图让左边看起来像右边。我有点迷茫了。有什么建议吗?
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0.5in]{geometry}
\pagestyle{empty}
\begin{document}
%\input{../tikz-setup.tex}
\usetikzlibrary{shapes, arrows, calc, positioning, matrix }
% Define block styles
\tikzstyle{line} = [ draw, -triangle 45 ]
\tikzstyle{list} = [ rectangle, draw,
minimum width = 6em
]
\tikzstyle{ptr-box} = [ rectangle,
text width = 8em
]
\begin{center}
\begin{tikzpicture}[align = flush center, font = \small]
% Place nodes
\matrix [matrix of nodes, row sep = 0em] (age)
{
|[list] (free)| Free Page \\
|[list] (free2)| Free Page \\
|[list] (pg6)| Page 6 \\
|[list] (pg12)| Page 12 \\
|[list] (pg4)| Page 4 \\
|[list] (pg8)| Page 8 \\
};
\matrix [matrix of nodes, row sep = 0em,
left = of age,
nodes={text width=6em},
matrix anchor = north east,
] (ptr) at (age.north west)
{
|[ptr-box, ] (oldest)| \textbf{oldest\_ptr} \\ \tiny{Points to pool of free pages.} \\
|[ptr-box] (oldest-valid)| \textbf{oldest\_valid\_ptr} \\ \tiny{Points to oldest vaild cache page, else NULL if cache is empty.} \\
|[ptr-box] (newest)|\textbf{newest\_ptr} \\ \tiny{Points to newest vaild cache page.} \\
};
% Draw edges
\path [line] (oldest) -- (free);
\path [line] (oldest-valid) -- (pg6);
\path [line] (newest) -- (pg8);
\end{tikzpicture}
\end{center}
\end{document}
答案1
一种选择是使用\parbox
es 作为包含文本的节点:
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0.5in]{geometry}
\pagestyle{empty}
\begin{document}
%\input{../tikz-setup.tex}
\usetikzlibrary{shapes, arrows, calc, positioning, matrix }
% Define block styles
\tikzstyle{line} = [ draw, -triangle 45 ]
\tikzstyle{list} = [ rectangle, draw,
minimum width = 6em
]
\tikzstyle{ptr-box} = [ rectangle,
text width = 30em , font=\tiny
]
\begin{center}
\begin{tikzpicture}[align = flush center, font = \small]
% Place nodes
\matrix [matrix of nodes, row sep = 0em] (age)
{
|[list] (free)| Free Page \\
|[list] (free2)| Free Page \\
|[list] (pg6)| Page 6 \\
|[list] (pg12)| Page 12 \\
|[list] (pg4)| Page 4 \\
|[list] (pg8)| Page 8 \\
};
\node [left=of free,text depth=0ex] (oldest) {\parbox[t]{8em}{\centering%
\textbf{oldest\_ptr} \\ \tiny Points to pool of free pages.}};
\node [left=of pg6,text depth=0ex] (oldest-valid) {\parbox[t]{8em}{\centering%
\textbf{oldest\_valid\_ptr} \\ \tiny Points to oldest vaild cache page, else NULL if cache is empty.}};
\node [left=of pg8,text depth=0ex] (newest) {\parbox[t]{8em}{\centering%
\textbf{newest\_ptr} \\ \tiny Points to newest vaild cache page.}};
% Draw edges
\path [line] (oldest) -- (free);
\path [line] (oldest-valid) -- (pg6);
\path [line] (newest) -- (pg8);
\end{tikzpicture}
\end{center}
\end{document}
另一种选择是使用具有适合文本节点选项的样式:
\documentclass{article}
\usepackage{tikz}
\usepackage[margin=0.5in]{geometry}
\usetikzlibrary{shapes, arrows, calc, positioning, matrix }
\pagestyle{empty}
\begin{document}
% Define block styles
\tikzstyle{line} = [ draw, -triangle 45 ]
\tikzstyle{list} = [ rectangle, draw,
minimum width = 6em
]
\tikzstyle{ptr-box} = [ rectangle,
text width = 30em , font=\tiny
]
\begin{center}
\begin{tikzpicture}[align = flush center, font = \small]
% Place nodes
\matrix [matrix of nodes, row sep = 0em] (age)
{
|[list] (free)| Free Page \\
|[list] (free2)| Free Page \\
|[list] (pg6)| Page 6 \\
|[list] (pg12)| Page 12 \\
|[list] (pg4)| Page 4 \\
|[list] (pg8)| Page 8 \\
};
\begin{scope}[
mytext/.style={
align=center, font=\tiny,xshift=-1em,anchor=north east,inner ysep=-1ex}
]
\node [left=of free,mytext] (oldest) {%
{\small\bfseries oldest\_ptr} \\ Points to pool of \\ free pages.};
\node [left=of pg6,mytext] (oldest-valid) {%
{\small\bfseries oldest\_valid\_ptr} \\ Points to oldest vaild \\ cache page, else NULL if \\ cache is empty.};
\node [left=of pg8,mytext] (newest) {%
{\small\bfseries newest\_ptr} \\ Points to newest vaild \\ cache page.};
\end{scope}
% Draw edges
\path [line] (oldest.north east) -- (free);
\path [line] (oldest-valid.north east) -- (pg6);
\path [line] (newest.north east) -- (pg8);
\end{tikzpicture}
\end{center}
\end{document}
顺便说一句,\tiny
它不接受参数;它是一个声明。
答案2
这是一种可能性(我修改了您的示例以使用\tikzset
而不是最旧的\tikzstyle
:应该使用 \tikzset 还是 \tikzstyle 来定义 TikZ 样式?):
\documentclass{standalone}
\usepackage{tikz}
% \input{../tikz-setup.tex}
\usetikzlibrary{shapes, arrows, calc, positioning, matrix }
% Define block styles
\tikzset{
line/.style = { draw, -triangle 45 },
list/.style = { rectangle, draw, minimum width = 6em },
ptr-box/.style = { rectangle, text width = 30em , font=\tiny },
comment/.style = {text width=2.5cm, align=center,inner ysep=0},
note/.style = {font=\tiny,align=center,text width=2.5cm,inner ysep=0},
}
\begin{document}
\begin{tikzpicture}[align=flush center,font=\small]
% Place nodes
\matrix [matrix of nodes, row sep = 0em] (age)
{
|[list] (free)| Free Page \\
|[list] (free2)| Free Page \\
|[list] (pg6)| Page 6 \\
|[list] (pg12)| Page 12 \\
|[list] (pg4)| Page 4 \\
|[list] (pg8)| Page 8 \\
};
\node [left=of free, comment] (oldest) {\textbf{oldest\_ptr}};
\node [below=1pt of oldest,note]{Points to pool of free pages.};
\node [left=of pg6, comment] (oldest-valid) {\textbf{oldest\_valid\_ptr}};
\node [below=1pt of oldest-valid,note]
{Points to oldest valid cache page, else NULL if cache is empty};
\node [left=of pg8,comment] (newest) {\textbf{newest\_ptr}};
\node [below=1pt of newest,note]{Points to newest vaild cache page.};
% Draw edges
\path [line] (oldest) -- (free);
\path [line] (oldest-valid) -- (pg6);
\path [line] (newest) -- (pg8);
\end{tikzpicture}
\end{document}