Tenho alguns valores que preciso colocar no formato de linha maior, ou seja, preencher a coluna de uma tabela com base em sua largura, conforme descrito no MWE.
\documentclass[a4paper]{article}
\usepackage[margin=2cm]{geometry} % Just for this example
\setlength{\tabcolsep}{0.5cm}
\newcommand\myelement{\rule{1cm}{0.5cm}}
\begin{document}
Suppose each element width is 1 cm.
The whole page is 21 cm - 2*2 cm = 19 cm and therefore can contain
(19 cm / (1 cm + 2*0.5 cm)) = 9.5 = 9 elements, so the output should be like:
\begin{tabular}{rrrrrrrrr}
\myelement & \myelement & \myelement & \myelement & \myelement & \myelement & \myelement & \myelement & \myelement\\
\myelement & \myelement & \myelement & \myelement & \myelement & \myelement & \myelement & \myelement & \myelement\\
\myelement & \myelement & \myelement
\end{tabular}
~\\[1cm]
\begin{minipage}{0.5\linewidth}
Here the output should be like:
\begin{tabular}{rrrr}
\myelement & \myelement & \myelement & \myelement \\
\myelement & \myelement & \myelement & \myelement \\
\myelement & \myelement & \myelement & \myelement \\
\myelement & \myelement & \myelement & \myelement \\
\myelement & \myelement & \myelement & \myelement \\
\myelement
\end{tabular}
\end{minipage}
\end{document}
O número de colunas deve ser deduzido automaticamente da largura da caixa que contém o "ambiente/comando autocolumn" (nomeie como desejar), desde que a largura máxima dos elementos.
Qualquer ideia? Eu poderia usar LuaLaTeX, mas esta solução seria muito lenta e consumiria muita memória, então esta é a última opção.
Responder1
\documentclass[a4paper]{article}
\usepackage[margin=2cm]{geometry} % Just for this example
\setlength{\tabcolsep}{0.5cm}
\newcommand\myelement{\rule{1cm}{0.5cm}}
\newcommand\xelement{\makebox[2cm]{\myelement}\hspace{0pt}}
\begin{document}
Suppose each element width is 1 cm.
The whole page is 21 cm - 2*2 cm = 19 cm and therefore can contain
(19 cm / (1 cm + 2*0.5 cm)) = 9.5 = 9 elements, so the output should be like:
\begin{flushleft}
\xelement \xelement \xelement \xelement \xelement \xelement \xelement \xelement \xelement
\xelement \xelement \xelement \xelement \xelement \xelement \xelement \xelement \xelement
\xelement \xelement \xelement
\end{flushleft}
\bigskip
\begin{minipage}{0.5\linewidth}
Here the output should be like:
\begin{flushleft}
\xelement \xelement \xelement \xelement
\xelement \xelement \xelement \xelement
\xelement \xelement \xelement \xelement
\xelement \xelement \xelement \xelement
\xelement \xelement \xelement \xelement
\xelement
\end{flushleft}
\end{minipage}
\end{document}