
Eu tenho uma grade/tabela/matriz com separadorótulospara as linhas e as colunas. No entanto, também quero rotular esses rótulos (indicando o "tipo" dos rótulos, como unidades SI, etc.)
Aqui está um MWE, com três variantes (verifique a entrada abc/def
na posição superior esquerda, descrevendo os rótulos das linhas abc
e os rótulos das colunas def
):
\documentclass{article}
\begin{document}
\begin{table}
\caption{Forward slash.}
\[\begin{array}{c|ccccc}
abc/def & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\begin{table}
\caption{Vertical bar.}
\[\begin{array}{c|ccccc}
abc|def & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\begin{table}
\caption{Backslash.}
\[\begin{array}{c|ccccc}
abc\backslash def & 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\end{document}
No entanto, este MWE sofre (pelo menos) dos dois problemas a seguir: a primeira coluna (rotulando as linhas) é muito larga e os rótulos centralizados estão longe do núcleo da matriz em si (o alinhamento à direita pode ajudar, mas.. . que introduz outros problemas). Segundo, o espaçamento em torno de abc/def parece incorreto.
A questão é, basicamente, como devo compor bem esse array? O que devo colocar na posição superior esquerda, no lugar de abc/def
? Não desejo colocar abc
e def
em duas (multi) colunas distintas. Talvez empilhar abc
acima (ou abaixo?) def
seja um começo, mas eu ainda precisaria de algum tipo de separador visual (assumindo o papel dos \|/
símbolos) dentro dessa célula. Provavelmente também deveria esclarecer que ambos os rótulos abc
e def
são, na prática,muito curto, normalmente eu escreveria x/y
or n/m
or n/k
, variáveis matemáticas de um único caractere, e não textos longos.
Relacionado:Como criar tabela com rótulos (primeira linha e primeira coluna)
Responder1
Vou sugerir "nenhuma das três possibilidades". Em vez disso, considere usar uma configuração clássica de “tableau”, com uma estrutura hierárquica clara no cabeçalho. Essa configuração ajuda a evitar a criação da aparência “apertada” que é quase inevitável com qualquer uma das três possibilidades que você ofereceu.
\documentclass{article}
\usepackage{booktabs}% for \toprule, \midrule, \bottomrule, and \cmidrule macros
\usepackage{amsmath} % for \text macro
\begin{document}
\begin{table}
\caption{Still another approach}
\[
\begin{array}{@{}l*{5}{c}@{}}
\toprule
\text{abc} & \multicolumn{5}{c@{}}{\text{def}}\\
\cmidrule(l){2-6}
& 1 & 2 & 3 & 4 & 5\\
\midrule
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\bottomrule
\end{array}
\]
\end{table}
\end{document}
Responder2
Também é possível usar TikZ diretamente dentro da tabela, sem precisar de nenhum savebox para obter os comprimentos:
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{table}
\caption{Diagonal Split}
\[\begin{array}{c|ccccc}
\tikz{\node[below left, inner sep=1pt] (def) {def};%
\node[above right,inner sep=1pt] (abc) {abc};%
\draw (def.north west|-abc.north west) -- (def.south east-|abc.south east);}
& 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\end{document}
Então, dentro do \tikz
comando você tem o TikPoder Z para fazer qualquer coisa... Aqui está uma versão com nós reposicionados, uma very thin
shorten
linha de divisão ed e fontes em \footnotesize
.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{table}
\caption{Diagonal Split}
\[\begin{array}{c|ccccc}
\tikz[diag text/.style={inner sep=0pt, font=\footnotesize},
shorten/.style={shorten <=#1,shorten >=#1}]{%
\node[below left, diag text] (def) {def};
\node[above right=2pt and -2pt, diag text] (abc) {abc};
\draw[shorten=4pt, very thin] (def.north west|-abc.north west) -- (def.south east-|abc.south east);}
& 1 & 2 & 3 & 4 & 5\\
\hline
1 & a & b & c & d & e\\
2 & f & g & h & i & j\\
3 & k & l & m & n & o\\
\end{array}\]
\end{table}
\end{document}
Responder3
Aqui estão duas opções:
\documentclass{article}
\usepackage{mathtools,eqparbox}
\newcommand{\indices}[2]{{% \indices{<rows>}{<columns>}
\begin{array}{@{}r@{}}
\scriptstyle #2~\smash{\eqmakebox[ind]{$\scriptstyle\rightarrow$}} \\[-\jot]
\scriptstyle #1~\smash{\eqmakebox[ind]{$\scriptstyle\downarrow$}}
\end{array}}}
\begin{document}
\[
\begin{array}{c|ccccc}
\indices{\text{abc}}{\text{def}}
& 1 & 2 & 3 & 4 & 5 \\
\hline
1 & a & b & c & d & e \\
2 & f & g & h & i & j \\
3 & k & l & m & n & o
\end{array}
\]
\[
\begin{array}{cc|ccccc}
&\multicolumn{1}{c}{} & \multicolumn{5}{c}{\text{def}} \\
&& 1 & 2 & 3 & 4 & 5 \\
\cline{2-7}
& 1 & a & b & c & d & e \\
\smash{\rotatebox[origin=c]{90}{\text{abc}}} & 2 & f & g & h & i & j \\
& 3 & k & l & m & n & o
\end{array}
\]
\end{document}
Se você estiver interessado em um comando semelhante a uma matriz, existem alguns exemplos emOnde está o \matrix
comando?. Isso inclui usar \bordermatrix
,kbordermatrix
eblkarray
, tudo isso permite colocar índices para identificar as linhas/colunas.
Responder4
Às vezes uso linhas diagonais ou caixas pretas:
\documentclass{article}
\usepackage{fp}
\usepackage{graphicx}
\newbox\MytempboxA
\newbox\MytempboxB
\newcommand\myTempA{}
\newcommand\myTempB{}
\newcommand\myTempC{}
\newcommand\myTempD{}
\begin{document}
\begin{table}%
\caption{Diagonal line approach}%
\[%
\setbox\MytempboxA\hbox{\mbox{abc}}%
\setbox\MytempboxB\hbox{\mbox{def}}%
\begin{array}{@{}|r|*{5}{c}|}
\hline
\multicolumn{1}{|l}{%
\edef\myTempA{%
\number\numexpr\dimexpr\wd\MytempboxA+2\arraycolsep\relax\relax
}%
\edef\myTempB{%
\number
\numexpr
\dimexpr\dp\csname @arstrutbox\endcsname+%
\ht\csname @arstrutbox\endcsname+%
\arrayrulewidth
\relax
\relax
}%
\FPpow\myTempC\myTempA{2}%
\FPpow\myTempD\myTempB{2}%
\FPadd\myTempC\myTempC\myTempD
\FProot\myTempC\myTempC{2}% length of diagonal line in sp
\FPdiv\myTempD\myTempB\myTempA
\FParctan\myTempD\myTempD% angle of diagonal line in rad
\smash{%
\kern-\arraycolsep
\rlap{%
\lower
\dimexpr
\dp\csname @arstrutbox\endcsname+\arrayrulewidth
\relax
\hbox{%
\rotatebox[units=-6.283185,origin=br]{\myTempD}{%
\rule{\myTempC sp}{\arrayrulewidth}%
}%
}%
}%
}%
}&\multicolumn{5}{c|}{\copy\MytempboxB}\\%
\cline{2-6}%
\copy\MytempboxA& 1 & 2 & 3 & 4 & 5\\%
\hline
1 & a & b & c & d & e\\%
2 & f & g & h & i & j\\%
3 & k & l & m & n & o\\%
\hline
\end{array}%
\]%
\end{table}
\begin{table}
\caption{Black box approach}%
\setbox\MytempboxA\hbox{\mbox{abc}}%
\setbox\MytempboxB\hbox{\mbox{def}}%
\[%
\begin{array}{@{}|r|*{5}{c}|}%
\hline
\multicolumn{1}{|l|}{%
\smash{%
\kern-\arraycolsep
\rlap{%
\rule[{-\dp\csname @arstrutbox\endcsname}]%
{\dimexpr\wd\MytempboxA+2\arraycolsep\relax}%
{%
\dimexpr
\dp\csname @arstrutbox\endcsname+%
\ht\csname @arstrutbox\endcsname
\relax
}%
}%
}%
}&\multicolumn{5}{c|}{\copy\MytempboxB}\\
\hline
\copy\MytempboxA& 1 & 2 & 3 & 4 & 5\\%
\hline
1 & a & b & c & d & e\\%
2 & f & g & h & i & j\\%
3 & k & l & m & n & o\\%
\hline
\end{array}%
\]%
\end{table}
\begin{table}%
\caption{Tangram puzzle approach}%
\[%
\setbox\MytempboxA\hbox{\mbox{abc}}%
\setbox\MytempboxB\hbox{\mbox{def}}%
\begin{array}{@{}|r|*{5}{c}|}
\hline
\multicolumn{1}{|l}{%
\edef\myTempA{%
\number\numexpr\dimexpr\wd\MytempboxA+2\arraycolsep\relax\relax
}%
\edef\myTempB{%
\number
\numexpr
\dimexpr\dp\csname @arstrutbox\endcsname+%
\ht\csname @arstrutbox\endcsname+%
\arrayrulewidth
\relax
\relax
}%
\FPpow\myTempC\myTempA{2}%
\FPpow\myTempD\myTempB{2}%
\FPadd\myTempC\myTempC\myTempD
\FProot\myTempC\myTempC{2}% length of diagonal line in sp
\FPdiv\myTempD\myTempB\myTempA
\FParctan\myTempD\myTempD% angle of diagonal line in rad
\smash{%
\kern-\arraycolsep
\rlap{%
\lower
\dimexpr
\dp\csname @arstrutbox\endcsname+\arrayrulewidth
\relax
\hbox{%
\rotatebox[units=-6.283185,origin=br]{\myTempD}{%
\rule{\myTempC sp}{\arrayrulewidth}%
}%
}%
}%
}%
}&\multicolumn{5}{c|}{\copy\MytempboxB}\\%
\copy\MytempboxA& 1 & 2 & 3 & 4 & 5\\%
\cline{2-6}%
1 & a & b & c & d & e\\%
2 & f & g & h & i & j\\%
3 & k & l & m & n & o\\%
\hline
\end{array}%
\]%
\end{table}
\begin{table}
\caption{squares approach}%
\setbox\MytempboxA\hbox{\mbox{abc}}%
\setbox\MytempboxB\hbox{\mbox{def}}%
\[%
\begin{array}{@{}|r|*{5}{c}|}%
\hline
\multicolumn{1}{|l|}{}&\multicolumn{5}{c|}{\copy\MytempboxB}\\
\copy\MytempboxA& 1 & 2 & 3 & 4 & 5\\%
\cline{2-6}%
1 & a & b & c & d & e\\%
2 & f & g & h & i & j\\%
3 & k & l & m & n & o\\%
\hline
\end{array}%
\]%
\end{table}
\end{document}
E às vezes eu uso algo como setas angulares:
\documentclass{article}
\usepackage{tikz}
\newbox\MytempboxA
\newbox\MytempboxB
\newbox\MytempboxC
\newcommand\Upbox[1]{%
\lower\dimexpr-\ht\MytempboxA+\ht\MytempboxB\relax\hbox{#1}%
%#1%
}%
\newcommand\leftbox[1]{%
\hbox to\wd\MytempboxC{\hss#1\hss}\hbox to\wd\MytempboxB{\hfill}%
}%
\begin{document}
\begin{table}%
\caption{Angle arrow.}%
\setbox\MytempboxB=\hbox{\mbox{def}}%
\setbox\MytempboxC=\hbox{\mbox{abc}}%
\setbox\MytempboxA=\hbox{\lower\dp\MytempboxC\vbox{%
\hbox{%
\begin{tikzpicture}[x=.25cm, y=.25cm, inner sep=0pt]
\draw[->,thin] (0,0) -- (1,0) node[right]{\copy\MytempboxB};
\draw[->,thin] (0,0) -- (0,-1) node[below]{\copy\MytempboxC};
\end{tikzpicture}%
}%
}}%
\[%
\begin{array}{c|ccccc}%
\copy\MytempboxA&\Upbox{1}&\Upbox{2}&\Upbox{3}&\Upbox{4}&\Upbox{5}\\%
\hline
\leftbox{1}& a & b & c & d & e\\%
\leftbox{2} & f & g & h & i & j\\%
\leftbox{3} & k & l & m & n & o\\%
\end{array}%
\]
\end{table}
\begin{table}
\caption{Another angle arrow.}%
\setbox\MytempboxB=\hbox{\mbox{def}}%
\setbox\MytempboxC=\hbox{\mbox{abc}}%
\[%
\begin{array}{c@{}c|ccccc}%
\smash{%
\hbox{%
\kern.5\wd\MytempboxC
\lower.75\ht\MytempboxB
\hbox{%
\begin{tikzpicture}[x=.25cm, y=.25cm, inner sep=0pt]
\draw[->,thin] (0,0) -- (1,0) node[right]{};
\draw[->,thin] (0,0) -- (0,-1) node[below]{};
\end{tikzpicture}%
}%
}%
}&\copy\MytempboxB&1&2&3&4&5\\%
\copy\MytempboxC&&\\%
\hline
1&& a & b & c & d & e\\%
2&& f & g & h & i & j\\%
3&& k & l & m & n & o\\%
\end{array}%
\]%
\end{table}
\end{document}
Você também pode criar a aparência dos eixos do sistema de coordenadas com linhas horizontais e verticais de uma matriz:
\documentclass{article}
\usepackage{tikz}
\newbox\MytempboxA
\newbox\MytempboxB
\newbox\MytempboxC
\begin{document}
\begin{table}%
\caption{Coordinate axes approach.}%
\setbox\MytempboxC=\hbox{\mbox{abc}}%
\setbox\MytempboxB=\hbox{\mbox{def}}%
\setbox\MytempboxA\hbox{%
\lower\arrayrulewidth\hbox{%
\begin{tikzpicture}[x=.25cm, y=.05cm, inner sep=0pt]%
\draw[->,line width=\arrayrulewidth] (0,1) -- (1,1) node[right]{};
\end{tikzpicture}%
}%
}%
\[%
\begin{array}{c|cccccl}%
&1&2&3&4&5&\\%
\cline{1-6}%
1& a & b & c & d & e&%
\kern\dimexpr-\arraycolsep-.5\arrayrulewidth\relax\null
\smash{%
\lower\dimexpr-\ht\csname @arstrutbox\endcsname+.75\arrayrulewidth+0pt\relax\copy\MytempboxA
\lower\dimexpr-\ht\csname @arstrutbox\endcsname+0.5ex+.75\arrayrulewidth\relax\copy\MytempboxB
}%
\\%
2 & f & g & h & i & j&\\%
3 & k & l & m & n & o&\\%
\multicolumn{1}{r}{}&%
\multicolumn{6}{l}{%
\kern\dimexpr-.5\wd\MytempboxC-\arraycolsep\relax\null
\vbox{%
\kern\dimexpr-.5\arrayrulewidth\relax\hbox{%
\begin{tikzpicture}[x=.25cm, y=.075cm, inner sep=0pt]%
\draw[->,line width=\arrayrulewidth] (0,1) -- (0,-1) node[below]{\copy\MytempboxC};
\end{tikzpicture}%
}%
}%
}%
\\%
\end{array}%
\]
\end{table}
\end{document}