如何处理环境主体并用 \macro 划分它?

如何处理环境主体并用 \macro 划分它?

我正在尝试使用环境自动执行以下布局以放置图像和表格minipage。环境的宽度minipage 是自动计算的,并且设计是几乎 自动,输出如下所示:

电流输出

使用以下代码(MWE):

\documentclass{article}
\usepackage{graphicx}
% Conter and style
\newcounter{ctr}
\renewcommand\thectr{(\Alph{ctr})}
% Macro to put * on the left, no need scope (used in other parts)
\newcommand{\fake}{\par\hspace*{-\labelsep}\makebox[0pt][r]{\makebox[\labelwidth][r]{\textasteriskcentered}}\hskip\labelsep}

\ExplSyntaxOn
\tl_new:N  \l_myenv_label_below_tl
\tl_set:Nn \l_myenv_label_below_tl { \thectr } % fake label
\int_new:N \l_myenv_layout_above_int
\int_new:N \l_myenv_layout_below_int
\dim_new:N \l_enumext_mini_page_width_dim
\dim_new:N \l_enumext_mini_page_width_below_dim
\clist_new:N \l_myenv_layout_clist

% Calc width for minipage
\cs_new_protected:Npn \__myenv_minipage_width:n #1
  {
    \clist_set:Nn \l_myenv_layout_clist {#1}
    \bool_if:nT { \int_compare_p:n { \clist_count:n {#1} = 1 } }
      {
        \int_set:Nn \l_myenv_layout_above_int { \clist_item:Nn \l_myenv_layout_clist {1} }
        \dim_set:Nn \l_enumext_mini_page_width_dim
          { \dim_use:c { linewidth } / \l_myenv_layout_above_int }
      }
    \bool_if:nT { \int_compare_p:n { \clist_count:n {#1} = 2 } }
      {
        \int_set:Nn \l_myenv_layout_above_int { \clist_item:Nn \l_myenv_layout_clist {1} }
        \dim_set:Nn \l_enumext_mini_page_width_dim
          { \dim_use:c { linewidth } / \l_myenv_layout_above_int }
        \int_set:Nn \l_myenv_layout_below_int { \clist_item:Nn \l_myenv_layout_clist {2} }
        \dim_set:Nn \l_enumext_mini_page_width_below_dim
          { \dim_use:c { linewidth } / \l_myenv_layout_below_int }
        % Adjusts the minipage width for the below part, scope \startbelow
        \NewDocumentCommand\startbelow{}
          {
            \dim_set_eq:NN \l_enumext_mini_page_width_dim \l_enumext_mini_page_width_below_dim
            \par
          }
      }
  }

% Create a environment, #1 = layout [above,below]
\NewDocumentEnvironment{picandtables}{ O{5} }
  {
    \__myenv_minipage_width:n {#1}
    % Scope \Item*[...]{...}
    \NewDocumentCommand\Item{ s O{} +m }
      {
        \refstepcounter{ctr}
        \noindent
        \begin{minipage}[b]{ \l_enumext_mini_page_width_dim }
          \centering
          ##3% picture or table
          \par
          \IfBooleanTF{##1}
            { ##2 \fake \tl_use:N \l_myenv_label_below_tl }% * on left
            { ##2 \par \tl_use:N \l_myenv_label_below_tl  }
        \end{minipage}
        \ignorespaces
      }
  } { \setcounter{ctr}{0} }
\ExplSyntaxOff
\begin{document}
% Common layout for images or tables
\begin{enumerate}
\item Type of questions (these need manual tuning for alternatives,
      usually carrying pictures or tables)

 \begin{picandtables}[3,2]
   \Item[note]{\includegraphics[scale=0.15]{example-image-a}}
   \Item{\includegraphics[scale=0.10]{example-image-a}}
   \Item*{\includegraphics[scale=0.15]{example-image-a}}
   \startbelow
   \Item{\includegraphics[scale=0.15]{example-image-a}}
   \Item{\includegraphics[scale=0.15]{example-image-a}}
 \end{picandtables}

\item Type of questions (these need manual tuning for alternatives,
      usually carrying pictures or tables)

  \begin{picandtables}[2,3]
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item[note]{\includegraphics[scale=0.15]{example-image-a}}
    \startbelow
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item*[note]{\includegraphics[scale=0.25]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
  \end{picandtables}

\item Type of questions (these need manual tuning for alternatives,
      usually carrying pictures or tables)

  \begin{picandtables}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item*{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item*{\includegraphics[scale=0.15]{example-image-a}}
    \Item[Note]{\includegraphics[scale=0.15]{example-image-a}}
  \end{picandtables}
\end{enumerate}
\end{document}

我想让它更自动化一些,目前环境的可选参数具有值5,但我并不总是有那么多\Item在环境中的命令,此外我必须放置命令\startbelow来划分多于以下 布局的一部分。

我的想法是让可选参数的默认值等于出现的次数\Item(这里我不需要,只是下面),用参数处理环境+b,计算出现的次数\Item,并在出现可选参数时将其除以[above, below],也就是说,不要使用\startbelow

我认为@egreg 给出的答案宏捕获直到行尾作为参数 符合我的要求,但是我不知道是否可行。

答案1

我定义\Item命令只是将相关内容添加到序列中。

然后,根据可选参数或(如果缺失)项目数,通过对列表进行映射来选择适当数量的项目。

因此,如果可选参数为[2,3],则首先将可用宽度除以二,然后将序列中的两个项目排版到所选大小的小页面中;然后将可用宽度除以三,再排版另外三个项目。

最后一个例子使用了十二个项目。

\documentclass{article}
\usepackage[top=1cm,bottom=2cm]{geometry} % smaller picture
\usepackage{graphicx}

\ExplSyntaxOn

\NewDocumentCommand{\Item}{som}
 {
  \seq_put_right:Nn \l_pablo_pictab_body_seq { \__pablo_pictab_item:nnn { #1 } { #2 } { #3 } }
 }

\NewDocumentEnvironment{picandtables}{o}
 {
  \par
  \centering
 }
 {
  \IfNoValueTF { #1 }
   {
    \pablo_pictab_do:e { \seq_count:N \l_pablo_pictab_body_seq }
   }
   {
    \pablo_pictab_do:n { #1 }
   }
  \par
 }

\seq_new:N \l_pablo_pictab_body_seq
\dim_new:N \l_pablo_pictab_width_dim
\int_new:N \l_pablo_pictab_label_int
\int_new:N \l_pablo_pictab_counta_int
\int_new:N \l_pablo_pictab_countb_int

\cs_new_protected:Nn \pablo_pictab_do:n
 {
  \clist_map_function:nN { #1 } \__pablo_pictab_row:n
 }
\cs_generate_variant:Nn \pablo_pictab_do:n { e }

\cs_new_protected:Nn \__pablo_pictab_row:n
 {
  \dim_set:Nn \l_pablo_pictab_width_dim { \linewidth/#1 }
  \int_set:Nn \l_pablo_pictab_counta_int { \l_pablo_pictab_countb_int }
  \int_set:Nn \l_pablo_pictab_countb_int { \l_pablo_pictab_counta_int + #1 }
  \int_step_inline:nnn { \l_pablo_pictab_counta_int + 1 } { \l_pablo_pictab_countb_int }
   {
    \int_incr:N \l_pablo_pictab_label_int
    \begin{minipage}[b]{\l_pablo_pictab_width_dim}
    \centering
    \seq_item:Nn \l_pablo_pictab_body_seq { ##1 }
    \end{minipage}
   }
  \par
 }

\cs_new_protected:Nn \__pablo_pictab_item:nnn
 {
  #3 \\
  \tl_if_novalue:nF { #2 } { #2 \\ }
  \bool_if:nT { #1 } { \makebox[0pt][r]{\textasteriskcentered\space} }
  (\int_to_Alph:n { \l_pablo_pictab_label_int })
 }

\ExplSyntaxOff
\begin{document}
% Common layout for images or tables
\begin{enumerate}
\item Type of questions (these need manual tuning for alternatives,
      usually carrying pictures or tables)

 \begin{picandtables}[3,2]
   \Item[note]{\includegraphics[scale=0.15]{example-image-a}}
   \Item{\includegraphics[scale=0.10]{example-image-a}}
   \Item*{\includegraphics[scale=0.15]{example-image-a}}
   \Item{\includegraphics[scale=0.15]{example-image-a}}
   \Item{\includegraphics[scale=0.15]{example-image-a}}
 \end{picandtables}

\item Type of questions (these need manual tuning for alternatives,
      usually carrying pictures or tables)

  \begin{picandtables}[2,3]
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item[note]{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item*[note]{\includegraphics[scale=0.25]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
  \end{picandtables}

\item Type of questions (these need manual tuning for alternatives,
      usually carrying pictures or tables)

  \begin{picandtables}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item*{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item*{\includegraphics[scale=0.15]{example-image-a}}
    \Item[Note]{\includegraphics[scale=0.15]{example-image-a}}
  \end{picandtables}

\item Type of questions (these need manual tuning for alternatives,
      usually carrying pictures or tables)

  \begin{picandtables}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item*{\includegraphics[scale=0.15]{example-image-a}}
    \Item[Note]{\includegraphics[scale=0.15]{example-image-a}}
  \end{picandtables}
\end{enumerate}

\clearpage

\begin{picandtables}[3,4,2,3]
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
    \Item{\includegraphics[scale=0.15]{example-image-a}}
\end{picandtables}

\end{document}

在此处输入图片描述

在此处输入图片描述

相关内容