8

I have two minipages and the problem that the height of a text line depends on the characters I am using. How can I always have the same height for a text line in a minipage?

I have used Tikz to demonstrate my problem. As you can see the baselines of each row are not aligned.

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}

\node[anchor=north west, inner sep=0, rectangle, red, draw, minimum width=0.5cm](A) {%
\begin{minipage}{0.5cm}%
x\\
x%
\end{minipage}};

\node[inner sep=0, rectangle, blue, draw, minimum width=0.5cm, right=0.1cm of A.north east, anchor=north west](B) {%
\begin{minipage}{0.5cm}%
M\\
M%
\end{minipage}};

\end{tikzpicture}
\end{document}

enter image description here

user4811
  • 4,185
  • Are you using minipages or tikz pictures? – Sigur Jan 07 '14 at 22:09
  • minipage are centred by default so baselines are not aligned, use \begin{minipage}[t] so they align on the baseline of the top row – David Carlisle Jan 07 '14 at 22:10
  • @Sigur: What do you mean? I am using minipages in tikz nodes. – user4811 Jan 07 '14 at 22:16
  • But why to use minipage inside a node since a node works like a minipage by itself? – Sigur Jan 07 '14 at 22:17
  • @DavidCarlisle: This doesn't solve my problem. The problem is that character "x" has a smaller height than character "M" so that Latex reserved less space for these text lines – user4811 Jan 07 '14 at 22:18
  • 2
    try adding \strut to all lines. – barbara beeton Jan 07 '14 at 22:19
  • @Sigur: I need line breaks. – user4811 Jan 07 '14 at 22:21
  • @barbarabeeton: Cool, this solved my problem but how can I set it for a whole minipage at once? (I have a lot of text and it would be quite painful to add \strut before every line). – user4811 Jan 07 '14 at 22:25
  • as long as what is contained in the minipage is entered as a "paragraph", and the type size has an appropriate baseline, you only need two struts -- one at the beginning and one at the end. you could probably even create a command that would manage that for you. – barbara beeton Jan 07 '14 at 22:32
  • @user4811 if [t] doesn't solve your problem you haven't described the problem well:-) if the tikz frame is only for debugging then the two minipages would align on the baselines and the line box holding them would be big enough for both so no extra space would need to be allocated. If you do want a visible frame then you need a strut as barbara said, or use a tabular p column (which adds the struts implictitly) – David Carlisle Jan 07 '14 at 22:39
  • @DavidCarlisle, that was the problem. I guess that OP want to put it inside a node. – Sigur Jan 07 '14 at 22:40
  • I guess that http://tex.stackexchange.com/a/124114/14757 can help you. – Sigur Jan 07 '14 at 22:48
  • @DavidCarlisle: \strut does the trick. I have adapted my example to show you what I wanted. – user4811 Jan 07 '14 at 23:17
  • @barbarabeeton: Feel free to make an answer out of your comments. I will then delete the solution in my question. – user4811 Jan 07 '14 at 23:19
  • @user4811 does your real example have the visible frame, or do you just need the baselines aligned? – David Carlisle Jan 07 '14 at 23:29
  • @DavidCarlisle: I just need the baselines aligned. My real Code is a protocol description. – user4811 Jan 07 '14 at 23:32
  • so you don't need the struts then:-) just remove all the tikz and put two minipage[t] side by side and they will align on baselines naturally (I'll add that to my answer) – David Carlisle Jan 07 '14 at 23:34
  • @DavidCarlisle: I don't need the visible frame but I do need Tikz to draw the rest of the protocol (arrows etc.). – user4811 Jan 08 '14 at 00:10

3 Answers3

5

It is not necessary to make the two boxes have the same height to align on the baselines, you just need to align on baselines rather than vertically centering the box.

If you want to make a visible frame around the box, then it is convenient to ensure they have the same height with a \strut , as shown in the second example.

If you don't need the visible tikz frames it is much easier, just place the minipage side by side, aligned on their top baseline as shown in the third example.

enter image description here

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}

\node[anchor = base west, inner sep=0, rectangle, red, draw, minimum width=0.5cm](A) {%
\begin{minipage}[t]{0.5cm}%
x\\
x%
\end{minipage}};

\node[anchor = base west, inner sep=0, rectangle, blue, draw, minimum width=0.5cm, base right=0.1cm of A.base east, ](B) {%
\begin{minipage}[t]{0.5cm}%
M\\
M%
\end{minipage}};

\end{tikzpicture}


\begin{tikzpicture}

\node[anchor = base west, inner sep=0, rectangle, red, draw, minimum width=0.5cm](A) {%
\begin{minipage}[t]{0.5cm}%
\strut x\\
x%
\end{minipage}};

\node[anchor = base west, inner sep=0, rectangle, blue, draw, minimum width=0.5cm, base right=0.1cm of A.base east, ](B) {%
\begin{minipage}[t]{0.5cm}%
\strut M\\
M%
\end{minipage}};

\end{tikzpicture}


\begin{minipage}[t]{0.5cm}%
x\\
x
\end{minipage}
\begin{minipage}[t]{0.5cm}%
M\\
M
\end{minipage}

\end{document}
David Carlisle
  • 757,742
4

as long as what is contained in the minipage is entered as a "paragraph", and the type size has an appropriate baseline, two struts, one at the beginning and one at the end would guarantee uniform treatment of the content. strictly speaking, the strut at the bottom is not usually necessary, but if the content is boxed, and you have multiple such boxes (such as in a flow chart), this will look better if the last line in one of the boxes has no descenders.

as noted in various comments, the option [t] is needed if the (baseline of the) top lines are to be the "hook" for positioning the boxes.

you could probably even create a command that would manage all the details.

\documentclass{article}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}

\node[anchor=north west, inner sep=0, rectangle, red, draw, minimum width=0.5cm](A) {%
\begin{minipage}[t]{0.5cm}%
\strut x\\
x\\
x\\
\strut x%
\end{minipage}};

\node[inner sep=0, rectangle, blue, draw, minimum width=0.5cm, right=0.1cm of A.north east, anchor=north west](B) {%
\begin{minipage}[t]{0.5cm}%
\strut M\\
M\\
M\\
\strut M%
\end{minipage}};

\end{tikzpicture}
\end{document}

enter image description here

user4811
  • 4,185
3

If you really want to use minipage inside a node you can insert a vertical rule with zero width on first line of both mini pages.

\begin{minipage}{0.5cm}
x\rule{0pt}{1cm} \\  %% choose the height
x
\end{minipage}

\begin{minipage}{0.5cm}
\rule{0pt}{1cm}M \\  %% choose the height
M
\end{minipage}

I used a colored rule with 0.4pt on the image below just to show the result.

enter image description here

enter image description here

enter image description here

Sigur
  • 37,330