Post by Zarko F. CucejPost by ClaudioPost by Zarko F. CucejPost by Alexander ZimmermannHi folks,
how can I vertically align the text in a node in regard to the baseline?
IMO the alignment looks ugly in the following example.
Alex
\documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[text centered,minimum height=2.2em, draw] {Bestätigte Daten};
\end{tikzpicture}
\end{document}
--
Dear Alex,
if I understood you correctly, your problem is described in TikZ manual.
See section 16.4.4 Text Parameters: Height and Depth of Text on page
----------------
In addition to changing the width of nodes, you can also change the
height of nodes. This can be done in two ways: First, you can use the
option minimum height, which ensures that the height of the whole node
is at least the given height (this option is described in more detail
later). Second, you can use the option text height, which sets the
height of the text itself, more precisely, of the TEX text box of the
text. Note that the text height typically is not the height of the
shape's box: In addition to the text height, an internal inner sep is
added as extra space and the text depth is also taken into account.
I recommend using minimum size instead of text height except for special
situations.
/tikz/text height=<dimension> (no default)
Sets the height of the text boxes in shapes. Thus, when you write
something like node {text}, the text is first typeset, resulting in some
box of a certain height. This height is then replaced by the height text
height. The resulting box is then used to determine the size of the
shape, which will typically be larger. When you write text height=
without specifying anything, the "natural" size of the text box remains
unchanged.
/tikz/text depth=<dimension> (no default)
This option works like text height, only for the depth of the text box.
This option is mostly useful when you need to ensure a uniform depth of
text boxes that need to be aligned.
----------------
Regarding to your example you already do what it can be done and result
is not look so ugly.
Regards, Zarko
Alex, Zarko,
I agree with Alex about the ugliness. More precisely there should be a
way to align baselines especially when there are downstems in the
text. Here is an example where the only solution I found is to use
text height.
Does anyone have a better idea ?
\begin{tikzpicture}[>=latex,rect/.style={shape=rectangle,rounded
corners, draw}]
\draw (0,2) node[rect] (src) {Data Source};
\draw[text height=0.9em] (0,1) node[rect] (enc) {Encoding};
\draw (0,0) node[rect] (tx) {Modulation};
\draw (3,0) node[rect] (ch) {Channel};
\draw (6,0) node[rect] (rx) {Demodulation};
\draw[text height=0.9em] (6,1) node[rect] (dec) {Decoding};
\draw (6,2) node[rect] (snk) {Data Sink};
\draw[->] (src) -- (enc);
\draw[->] (enc) -- (tx) ;
\draw[->] (tx) -- node[above] {$X$} (ch);
\draw[->] (ch) -- node[above] {$Y$} (rx);
\draw[->] (rx) -- (dec);
\draw[->] (dec) -- (snk);
\node [rect, fit=(src) (enc) (tx), label=above:Transmitter] {};
\node [rect, fit=(rx) (dec) (snk), label=above:Receiver] {};
\end{tikzpicture}
Best regards,
Claudio
Claudio,
what is ugly (without correction of text heights of boxes Encoding and
decoding) on the your example?
If you like to have all boxes with equal height and width, then you had
to specify explicitly in definition of rect style their height and width
and also placement of text in it. The same it is, if you like to have
more (or less) space between text in box lines. I guess that the
aforementioned confirm your findings.
For specifying of this measures you have more options: minimum height,
text height and depth, minimum width, text width, inner sep. For details
see manual.
\documentclass{article}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{arrows,fit}
\begin{document}
\begin{tikzpicture}[>=latex,
rect/.style={shape=rectangle, rounded corners, draw,
text height=1em, text depth=1ex, text width=15ex,
align=center, inner sep=1pt}]
% for "natural" width of text erase 'text width' and 'align'
% and adjust inner sep to desired distance separately for horizontal and
vertical direction (width inner 'xsep' and 'inner ysep')
\draw (0,2) node[rect] (src) {Data Source};
\draw (0,1) node[rect] (enc) {Encoding};
\draw (0,0) node[rect] (tx) {Modulation};
\draw (4,0) node[rect] (ch) {Channel};
\draw (8,0) node[rect] (rx) {Demodulation};
\draw (8,1) node[rect] (dec) {Decoding};
\draw (8,2) node[rect] (snk) {Data Sink};
\draw[->] (src) -- (enc);
\draw[->] (enc) -- (tx) ;
\draw[->] (tx) -- node[above] {$X$} (ch);
\draw[->] (ch) -- node[above] {$Y$} (rx);
\draw[->] (rx) -- (dec);
\draw[->] (dec) -- (snk);
\node [rect, inner sep=2mm, fit=(src) (enc) (tx),
label=above:Transmitter] {};
\node [rect, inner sep=2mm, fit=(rx) (dec) (snk), label=above:Receiver] {};
\end{tikzpicture}
\end{document}
regards, Zarko
Zarko,
thank you for your answer.
I'll try to explain what I find ugly in my example (without the text
height adjustments).
If you measure the distance between the lower border of the boxes and
the baseline of the text,you'll notice that for the Encoding and
Decoding (the only having down-stems) this distance is larger than for
all the other boxes. Which to me seems funny.
Actually you pointed me to the right direction using "text depth". The
same problem is described in the pgfmanual in section 5.1 page 57.
The code I retain is:
\documentclass{article}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{arrows,fit}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}%
\begin{document}
\begin{tikzpicture}[>=latex,
rect/.style={shape=rectangle, rounded corners, draw, text
height=0.8em, text depth=0.25ex,
%text width=15ex, text centered,
%inner sep=1pt,
}]
% for "natural" width of text erase 'text width' and 'align'
% and adjust inner sep to desired distance separately for horizontal
and
% vertical direction (width inner 'xsep' and 'inner ysep')
\draw (0,2) node[rect] (src) {Data Source};
\draw (0,1) node[rect] (enc) {Encoding};
\draw (0,0) node[rect] (tx) {Modulation};
\draw (4,0) node[rect] (ch) {Channel};
\draw (8,0) node[rect] (rx) {Demodulation};
\draw (8,1) node[rect] (dec) {Decoding};
\draw (8,2) node[rect] (snk) {Data Sink};
\draw[->] (src) -- (enc);
\draw[->] (enc) -- (tx) ;
\draw[->] (tx) -- node[above] {$X$} (ch);
\draw[->] (ch) -- node[above] {$Y$} (rx);
\draw[->] (rx) -- (dec);
\draw[->] (dec) -- (snk);
\node [rect, inner sep=2mm, fit=(src) (enc) (tx),
label=above:Transmitter] {};
\node [rect, inner sep=2mm, fit=(rx) (dec) (snk),
label=above:Receiver] {};
\end{tikzpicture}
\end{document}
Note however that text width=15ex does not play nice with the rest of
the options as when activated the text is shifted down by approx 1em.
Do you see the same issue ?
Thank you!
Claudio