使用文本宽度时将文本对齐到中心

使用文本宽度时将文本对齐到中心

我试图将固定宽度的矩形内的文本对齐到中心

\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}

在此处输入图片描述

我试过在里面使用延迟for tree,但没有效果

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

抛出下一个错误

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

我也尝试过\centering在文本前面添加,但没有任何效果

[\centering Con formación de imagen]

并且使用align=center不做任何事情,只是增加填充并避免文本中断

在此处输入图片描述

答案1

使用/tikz/align=center而不是align=center

后者是 Forest 特有的选项,可打开tabular环境。上面发生的情况是,您的文本在表格中居中,但表格在节点中不居中。

说实话,我很遗憾有一天我会推翻 TikZ 的 align......

答案2

除了包作者提出的解决方案之外, forest/first +1 是我的 ;-)/ 也有效text centered

编辑: 大部分都离题了。有两种使用它的情况,并且相当修改了你的代码:

\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}

给出:

在此处输入图片描述

相关内容