¿Cómo evitar que el texto vertical se salga de una tabla en LaTex?

¿Cómo evitar que el texto vertical se salga de una tabla en LaTex?

Soy nuevo en LaTex y tengo un pequeño problema con esta tabla... Tengo que hacer muchas tablas de ese tipo y me gustaría encontrar una manera de solucionar este problema automáticamente. ¿Puede alguien ayudarme por favor? A continuación encontrará mi código y la tabla.

¡Muchas gracias!

\documentclass{article}

\usepackage{multirow}
\usepackage{xcolor}
\usepackage{colortbl}
\usepackage{rotating}

\begin{document}
    \begin{table}[!ht]
        \begin{center}
            \caption{Matrice de confusion pour les classes texturales concernant notre campagne d’échantillonnage.}
            \begin{tabular}{|
            >{\columncolor[HTML]{C0C0C0}}c |
            >{\columncolor[HTML]{EFEFEF}}c |c|c|c|c|}   
            \hline
            \multicolumn{1}{|l|}{\cellcolor[HTML]{C0C0C0}} & \multicolumn{5}{c|}{\cellcolor[HTML]{C0C0C0}\textbf{\begin{tabular}[c]{@{}c@{}}Texture\\ observée sur le terrain\end{tabular}}} \\ \hline
            \cellcolor[HTML]{C0C0C0} &  & \cellcolor[HTML]{EFEFEF}A & \cellcolor[HTML]{EFEFEF}L & \cellcolor[HTML]{EFEFEF}Z & \cellcolor[HTML]{EFEFEF}\textbf{Total} \\ \cline{2-6} 
            \cellcolor[HTML]{C0C0C0} & A & 3 & 2 & 0 & 5 \\ \cline{2-6} 
            \cellcolor[HTML]{C0C0C0} & L & 4 & 7 & 0 & 11 \\ \cline{2-6} 
            \cellcolor[HTML]{C0C0C0} & Z & 0 & 1 & 3 & 4 \\ \cline{2-6} 
            \multirow{-5}{*}{\cellcolor[HTML]{C0C0C0}\textbf{\rotatebox{90}{Référence CNSW}}} & \textbf{Total} & 7 & 10 & 3 & \textbf{20} \\ \hline
            \end{tabular}
        \end{center}
    \end{table}
\end{document}

La mesa

Respuesta1

Siempre intentaría evitar este color excesivo de las tablas. Mantenlo limpio y ordenado, ¡al final será más legible!

Dicho esto, aquí hay una posible solución. Sin embargo , tenga en cuenta que debe ajustar manualmente la \parboxposición.

\documentclass{article}

\usepackage{multirow}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{rotating}

\renewcommand\theadalign{bc}
\renewcommand\theadfont{\bfseries}

\begin{document}
    \begin{table}[!ht]
        \caption{Matrice de confusion pour les classes texturales concernant notre campagne d’échantillonnage.}
        \begin{center}
            \begin{tabular}{*{6}{c}}
                \toprule
                    & \multicolumn{5}{c}{\thead{Texture\\ observée sur le terrain}}\\
                \midrule
                \multirow{5}{*}{\rotatebox{90}{\parbox[c]{2.2cm}{\centering\textbf{Référence CNSW}}}}
                    &  
                    & A 
                    & L 
                    & Z 
                    & \textbf{Total}\\
                    & A 
                    & 3 
                    & 2 
                    & 0 
                    & 5\\
                    & L 
                    & 4 
                    & 7 
                    & 0 
                    & 11\\
                    & Z 
                    & 0 
                    & 1 
                    & 3 
                    & 4\\\addlinespace
                    & \textbf{Total} 
                    & 7 
                    & 10 
                    & 3 
                    & \textbf{20} \\
                \bottomrule
            \end{tabular}
        \end{center}
    \end{table}
\end{document}

Mesa

Respuesta2

Con nicematrix, obtendrá un resultado perfecto en todos los visores de PDF en todos los niveles de zoom (no tendrá líneas blancas finas en los paneles grises y las reglas no parecerán desaparecer en algunos niveles de zoom).

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{caption}
\usepackage{nicematrix}

\NiceMatrixOptions{cell-space-top-limit=2pt}

\begin{document}

\begin{table}[!ht]
\centering
\captionsetup{width=9cm, format=hang}
\setlength{\extrarowheight}{2pt}
\caption{Matrice de confusion pour les classes texturales concernant notre campagne d’échantillonnage.}
\begin{NiceTabular}{cccccc}[hvlines,corners=NW]
\CodeBefore
  \rectanglecolor{LightGrey!50}{2-2}{2-6} 
\Body
  & \Block[fill=LightGrey]{1-5}<\bfseries>{Texture\\ observée sur le terrain} \\ 
  \Block[fill=LightGrey]{5-1}<\bfseries\rotate>{Référence\\CNSW} & 
  \Block[fill=LightGrey!50]{5-1}{} 
  & A & L & Z & \textbf{Total} \\
  & A & 3 & 2 & 0 & 5 \\
  & L & 4 & 7 & 0 & 11 \\
  & Z & 0 & 1 & 3 & 4 \\
  & \textbf{Total} & 7 & 10 & 3 & \textbf{20} \\ 
\end{NiceTabular}
\end{table}

\end{document}

Necesita varias compilaciones porque nicematrixutiliza nodos PGF/Tikz.

Salida del código anterior

Respuesta3

Aquí hay una solución, con varias mejoras y simplificación de código. Para su problema específico, la herramienta principal es el makecellpaquete, que permite saltos de línea en celdas estándar, excepto que no funciona bien con colores en tablas, porque hay tabulares detrás de escena. El \Centerstackcomando from stackengineno tiene este problema. Además, no tiene que cargar xcolory colortbl: la tableopción xcolor lo hace por usted y agrega algunos comandos de color más para las tablas. Por último, \clineen una tabla de colores quedan unas finas líneas blancas, así que las reemplacé con \hhlines para que parte de las líneas tengan el color del fondo, para hacerlas invisibles.

\documentclass{article}

\usepackage{multirow}
\usepackage[table, svgnames]{xcolor}
\colorlet{grey2}{LightGrey!25}
\usepackage{rotating}
\usepackage{caption, makecell, hhline}
\newcommand{\myhhline}{\hhline{|>{\arrayrulecolor{LightGrey}}- > {\arrayrulecolor{black}}|>{\arrayrulecolor{grey2}}->{\arrayrulecolor{black}}|----|}}
\usepackage[usestackEOL]{stackengine}

\begin{document}

    \begin{table}[!ht]
        \centering
        \captionsetup{width=9cm, format=hang}
        \setlength{\extrarowheight}{2pt}
            \caption{Matrice de confusion pour les classes texturales concernant notre campagne d’échantillonnage.}
            \begin{tabular}{|
            >{\columncolor{LightGrey}}c |
            >{\columncolor{grey2}}c|c|c|c|c|}
            \hline
            \rowcolor{LightGrey} & \multicolumn{5}{c|}{\bfseries\Centerstack{Texture\\ observée sur le terrain}} \\ \hline
            \rowcolor{grey2}\cellcolor{LightGrey} & & A & L & Z & \textbf{Total} \\[-0.2pt]
            \myhhline
             & A & 3 & 2 & 0 & 5 \\[-0.1pt]
            \myhhline%
            & L & 4 & 7 & 0 & 11 \\[-0.1pt]
            \myhhline
            & Z & 0 & 1 & 3 & 4 \\[-0.1pt]
            \myhhline
            \multirow{-5}{*}{\textbf{\rotatebox{90}{\Centerstack{Référence\\ CNSW}}}} & \textbf{Total} & 7 & 10 & 3 & \textbf{20} \\ \hline
            \end{tabular}
    \end{table}

\end{document}

ingrese la descripción de la imagen aquí

información relacionada