0

I would like to have some text vertically centered in a box.

I can get the text to be at the bottom of the box like this

\documentclass[]{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\fill[black, rounded corners=0.2cm] (0, 8) rectangle (8, 0)
            node[above left, white, text width=7.8cm, align=center] {Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.};
\end{tikzpicture}
\end{document}

But how do I get the text to be centered in the box?

2 Answers 2

2

Try: midway instead of above left:

\documentclass[]{standalone}

\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\fill[black, rounded corners=0.2cm] (0, 8) rectangle (8, 0)
            node[midway, white, text width=7.8cm, align=center] {Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.};
\end{tikzpicture}
\end{document}

enter image description here

1

Instead draw rectangle and then insert node with text, I sugest just draw node with text:

\documentclass[border=3.141592]{standalone}
\usepackage{microtype}
%\usepackage{newtxtext}

\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
N/.style = {fill=black, text=white, rounded corners=2mm, 
            minimum size=80mm, 
            text width=\pgfkeysvalueof{/pgf/minimum width}-2*\pgfkeysvalueof{/pgf/inner xsep},
            align=center}
                        ]
\node[N] {Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.};
    \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .