如何将文本定位到节点顶部?
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{every node/.style={rounded corners,minimum height=5cm}}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {
\begin{minipage}{5cm}
alignment of nodes
with \verb|minipage|
long text
fourth line
\end{minipage}
};
\draw[<-,>=latex,red,thick] (-2.5,2.4) to[bend left] (-2.5,3) node[right,blue] {i need text here.};
\end{tikzpicture}
\end{document}
答案1
环境minipage
有几个可选参数:
[pos]
t
取、b
、之一c
,分别将 定位minipage
在周围文本行的顶部、底部或中心[height]
指定高度minipage
[inner pos]
t
再次采用、b
、之一c
,但这次它指定了内容在minipage
就你的情况而言,你可以使用
\begin{minipage}[t][5cm]{5cm} ... \end{minipage}
定位文本。然后您的节点将具有 的宽度和高度5cm + 2*inner sep
。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{every node/.style={rounded corners}}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {
\begin{minipage}[t][5cm]{5cm}
alignment of nodes
with \verb|minipage|
long text
fourth line
\end{minipage}
};
\end{tikzpicture}
\end{document}
答案2
Jake 的回答很好,但如果你真的想遵守minimum height
,即允许节点也更大,你需要自己测量高度(实际上是深度),并在需要时增加它。你可以为此编写一个自己的环境,将宽度和总高度作为两个参数。
minimum height
还可以通过使用读出参数来替换高度参数\pgfmathsetlength\pgf@yb{\pgfkeysvalueof{/pgf/minimum height}}%
。
\documentclass{report}
\usepackage{tikz}
\usetikzlibrary{positioning}
\makeatletter
\newenvironment{minsizebox}[2]{%
\pgfmathsetlength\@tempdima{#2}%
\pgfmathsetlength\pgf@yc{\pgfkeysvalueof{/pgf/inner ysep}}%
\advance\@tempdima by -2\pgf@yc
\begin{lrbox}{\@tempboxa}%
\begin{minipage}[t]{#1}%
\vspace{0pt}%
}{%
\end{minipage}%
\end{lrbox}%
\ifdim\@tempdima>\dp\@tempboxa
\dp\@tempboxa=\@tempdima
\fi
\box\@tempboxa
}
\makeatother
\begin{document}
\tikzset{every node/.style={rounded corners,minimum height=5cm}}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {%
\begin{minsizebox}{5cm}{5cm}
alignment of nodes
with \verb|minipage|
long text
fourth line
\end{minsizebox}%
};
\end{tikzpicture}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {%
\begin{minsizebox}{5cm}{5cm}
alignment of nodes
with \verb|minipage|
long text
long text
long text
long text
long text
long text
long text
\end{minsizebox}%
};
\end{tikzpicture}
\begin{tikzpicture}
\node[draw,fill=yellow] (n1) {%
\begin{minsizebox}{5cm}{5cm}
alignment of nodes
with \verb|minipage|
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
long text
\end{minsizebox}%
};
\end{tikzpicture}
\end{document}