Como evitar que o texto vertical saia de uma tabela no LaTex?

Como evitar que o texto vertical saia de uma tabela no LaTex?

Sou novo no LaTex e estou com um probleminha com essa tabela... Tenho que fazer muitas tabelas desse tipo e gostaria de encontrar uma maneira de corrigir esse problema automaticamente. Alguém pode me ajudar, por favor? Abaixo você encontra meu código e a tabela.

Muito obrigado!

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

A mesa

Responder1

Eu sempre tentaria evitar essa coloração excessiva das mesas. Mantenha-o limpo e arrumado, no final fica mais legível!

Dito isto, aqui está uma solução possível. Observe que você precisa ajustar manualmente a \parboxposição.

\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

Responder2

Com nicematrixvocê terá uma saída perfeita em todos os visualizadores de PDF em todos os níveis de zoom (você não terá linhas brancas finas nos painéis cinza e as regras não parecerão desaparecer em alguns níveis 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}

Você precisa de várias compilações porque nicematrixusa nós PGF/Tikz.

Saída do código acima

Responder3

Aqui está uma solução, com diversas melhorias e simplificação de código. Para o seu problema específico, a principal ferramenta é o makecellpacote, que permite quebras de linha em células padrão, exceto que não funciona bem com cores em tabelas, pois existem tabulares nos bastidores. O \Centerstackcomando from stackenginenão tem esse problema. Além disso, você não precisa carregar xcolorand colortbl: a tableopção xcolor faz isso por você e adiciona mais alguns comandos de cores para tabelas. Por último, \clineem uma tabela colorida saem algumas linhas brancas finas, então substituí-as por \hhlines para que parte das linhas tenha a cor de fundo, de modo a torná-las invisíveis.

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

insira a descrição da imagem aqui

informação relacionada