Horizontal-Alignment,Forest – How to Center Align Text Using Text Width in Forest

foresthorizontal alignment

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

Best Answer

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 ...