2

I'm trying to align to the center the text inside rectangles of fixed width

\documentclass[12pt]{report}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Packages

\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows.meta, calc, patterns, patterns.meta, shadows}
\usepackage[edges]{forest}
\usepackage{xcolor}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Colors

\definecolor{lilac}{RGB}{174, 182, 211}
\definecolor{lightblue}{RGB}{176, 221, 255}
\definecolor{lightpink}{RGB}{245, 160, 240}
\definecolor{lightgreen}{RGB}{150, 240, 180}
\definecolor{linecol}{RGB}{92, 92, 92}
\definecolor{background}{RGB}{239, 239, 239}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}
\begin{forest}
    for tree={
        line width=0.5pt,
        draw=linecol,
        rect,
        calign=center,
        minimum size=25pt,
        text width=50mm,
        rounded corners=5pt,
        child anchor=north,
        parent anchor=south,
        drop shadow,
        l sep+=12.5pt,
        inner color=background,
        anchor=center,
        edge path={
            \noexpand\path[color=linecol, rounded corners=5pt,>={Stealth[length=10pt]}, line width=0.5pt, ->, \forestoption{edge}]
            (!u.parent anchor) -- +(0,-5pt) -|
            (.child anchor)\forestoption{edge label};
        },
        if = {level == 1}{outer color=lightblue, text width=38mm}{},
        if = {level == 2}{outer color=lightgreen, text width=28mm, child anchor=west}{}
    }
    [Clasificación, outer color=lilac,
    for tree = {
        for children = {
            grow'=0,
            folder,
        }
    }
        [Principio óptico,
            [Reflexión]
            [Refracción]
            [Dispersión]
            [\Gls{fluorescencia}]
        ]
        [Imagen,
            [Con formación de imagen, inner color=background, outer color=lightgreen]
            [Sin formación de imagen, inner color=background, outer color=lightgreen]
        ]
        [Concentración,
            [A lo largo de una línea]
            [En un único punto]
        ]
    ]
\end{forest}
\end{document}

enter image description here

I've tried using delay inside for tree but it didn't work

delay={content={\centering #1}}

Throwing the next error

! Illegal parameter number in definition of \pgfkeyscurrentkey.
<to be read again>
1
l.127 ^^I^^I^^I^^I\end{forest}
You meant to type ## instead of #, right?
Or maybe a } was forgotten somewhere earlier, and things
are all screwed up? I'm going to assume that you meant ##.
! Illegal parameter number in definition of \pgfkeyscurrentkeyRAW.
<to be read again>
1
l.127 ^^I^^I^^I^^I\end{forest}
You meant to type ## instead of #, right?
...

I've also tried putting \centering before the text but it did nothing

[\centering Con formación de imagen]

And using align=center does nothing but increase padding and avoid textbreak

enter image description here

2 Answers 2

5

Use /tikz/align=center rather than align=center.

The latter is a Forest-specific option which opens a tabular environment. What happens above is that your text is centered within the table, but the table is not centered within the node.

To be honest, I lament the day I overrode TikZ's align ...

3

Beside solution proposed by forest package author /first +1 is mine ;-)/ also works text centered.

Edit: mostly off-topic. Two cases of using it and quite modified your code:

\documentclass[border=3.141592mm]{standalone}
\usepackage{xcolor}
\definecolor{lilac}{RGB}{174, 182, 211}
\definecolor{lightblue}{RGB}{176, 221, 255}
\definecolor{lightgreen}{RGB}{150, 240, 180}
\definecolor{background}{RGB}{239, 239, 239}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta, shadows}

\begin{document}
As is shown in question:

    \begin{forest}
    for tree={
        draw, rounded corners,
        font=\small\linespread{0.84}\selectfont,
        text width=44mm,
        text centered,  % or as suggested package outhor: /tikz/align=center
        inner color=background,
        outer color=lilac,
if level =1{draw,
            outer color=cyan!30,
            text width=33mm,
            edge={draw, rounded corners, -Stealth},
            edge path={\noexpand\path[\forestoption{edge}]
            (!u.south) -- ++ (0,-2mm) -| (.child anchor);}
            }{},
if level>=1{grow'=0,
            folder,
            s sep=1mm
            }{},
if level =2{text width=22mm,
            outer color=green!30}{},
              }
%
[Clasificación, %outer color=lilac,
    [Principio óptico, 
        [Reflexión]
            [Refracción]
            [Dispersión]
            [{fluorescencia}]
        ]
        [Imagen,
            [Con formación de imagen]
            [Sin formación de imagen]
        ]
        [Concentración,
            [A lo largo de una línea]
            [En un único punto]
        ]
]
    \end{forest}

\vspace{2\baselineskip}
with centered root:

    \begin{forest}
    for tree={
        draw, rounded corners,
        font=\small\linespread{0.84}\selectfont,
        text width=44mm,
        text centered,  % or as suggested package outhor: /tikz/align=center
        inner color=background,
        outer color=lilac,
        calign=edge midpoint,
if level =1{draw,
            outer color=cyan!30,
            text width=33mm,
            edge={draw, rounded corners, -Stealth},
            edge path={\noexpand\path[\forestoption{edge}]
            (!u.south) -- ++ (0,-2mm) -| (.child anchor);}
            }{},
if level>=1{grow'=0,
            folder,
            s sep=1mm
            }{},
if level =2{text width=22mm,
            outer color=green!30}{},
              }
%
[Clasificación, name=root
    [Principio óptico,
        [Reflexión]
            [Refracción]
            [Dispersión]
            [{fluorescencia}]
        ]
        [Imagen, name=mid, no edge, 
            [Con formación de imagen]
            [Sin formación de imagen]
        ]
        [Concentración,
            [A lo largo de una línea]
            [En un único punto]
        ]
]
\draw[-Stealth] (root) -- (mid);
    \end{forest}
\end{document}

gives:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.