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}
요소의 최대 너비가 제공되면 "자동 열 환경/명령"(원하는 이름)이 포함된 상자 너비에서 열 수를 자동으로 추론해야 합니다.
어떤 아이디어? LuaLaTeX를 사용할 수도 있지만 이 솔루션은 너무 느리고 메모리가 부족하므로 이것이 마지막 옵션입니다.
답변1
\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}