阅读学生班级名单

阅读学生班级名单

每当我创建学生班级列表时,我都会使用下面 MWE 中的格式。我遇到的问题是,我通常必须一次复制并粘贴一个学生姓名,并确保每次表格都正确。

是否可以从列表或文件中读取学生姓名并自动创建至少有 20 行的表格,将第 4 列中的第 21 个条目推送到该表格,如下所示。如果班级名单少于 20 个,则仅创建三列,包括#Student NameSignature

以下是 MWE 或期望输出:

\documentclass{article}
\usepackage{array}
\begin{document}
\begin{center}
\renewcommand{\arraystretch}{1.3}
\makebox[\linewidth]{%
    \begin{tabular}{|r|l|>{\centering\arraybackslash}p{6em}|r|l|>{\centering\arraybackslash}p{6em}|}
    \hline
        \# & \textbf{Student Name} & \textbf{Signature} & \# & \textbf{Student Name} & \textbf{Signature}\\
    \hline
        1 & Surname, Fname Mname            & & 18 & Surname, Fname Mname &\\
    \hline
        2 & Surname, Fname Mname            & & 19 & Surname, Fname Mname &\\
    \hline
        3 & Surname, Fname Mname            & & 20 & Surname, Fname Mname &\\
    \hline
        4 & Surname, Fname Mname            & & 21 & Surname, Fname Mname &\\
    \hline
        5 & Surname, Fname Mname            & & 22 & Surname, Fname Mname &\\
    \hline
        6 & Surname, Fname Mname            & & 23 & Surname, Fname Mname &\\
    \hline
        7 & Surname, Fname Mname            & & 24 & Surname, Fname Mname &\\
    \hline
        8 & Surname, Fname Mname            & & 25 & Surname, Fname Mname &\\
    \hline
        9 & Surname, Fname Mname            & & 26 & Surname, Fname Mname &\\
    \hline
        10 & Surname, Fname Mname           & & 27 & Surname, Fname Mname &\\
    \hline
        11 & Surname, Fname Mname           & & 28 & Surname, Fname Mname &\\
    \hline
        12 & Surname, Fname Mname           & & 29 & Surname, Fname Mname &\\
    \hline
        13 & Surname, Fname Mname           & & 30 & Surname, Fname Mname &\\
    \hline
        14 & Surname, Fname Mname           & & 31 & Surname, Fname Mname &\\
    \hline
        15 & Surname, Fname Mname           & & 32 & Surname, Fname Mname &\\
    \hline
        16 & Surname, Fname Mname           & & 33 & Surname, Fname Mname &\\
    \hline
        17 & Surname, Fname Mname           & & 34 & Surname, Fname Mname &\\
    \hline
    \end{tabular}}
\end{center}
\end{document}

在此处输入图片描述

在此处输入图片描述

请注意,学生的最大人数为 40 人。

我读了@egreg 的回答https://tex.stackexchange.com/a/499035/10898但它似乎有点复杂。我知道我可能需要使用它,filecontents但我无法以表格格式使用它。

答案1

是的,您可以从文件中读取并以相当直接的方式构建表格。

\documentclass{article}
\usepackage[margin=1cm]{geometry} % make room
\usepackage{xparse,array}

\ExplSyntaxOn

\NewDocumentCommand{\studentlist}{m}
 {% #1 = file name
  \azetina_studentlist:n { #1 }
 }

\ior_new:N \g_azetina_studentlist_stream
\seq_new:N \l_azetina_studentlist_seq
\seq_new:N \l__azetina_studentlist_temp_seq
\tl_new:N \l__azetina_studentlist_header_tl

\cs_new_protected:Nn \azetina_studentlist:n
 {
  \seq_clear:N \l_azetina_studentlist_seq
  \ior_open:Nn \g_azetina_studentlist_stream { #1 }
  \int_zero:N \l_tmpa_int
  % populate the sequence
  \ior_map_inline:Nn \g_azetina_studentlist_stream
   {
    \int_incr:N \l_tmpa_int
    \seq_put_right:Nx \l_azetina_studentlist_seq
     {
      \int_to_arabic:n { \l_tmpa_int } & \exp_not:n { ##1 } &
     }
   }
  % build the header
  \tl_set:Nn \l__azetina_studentlist_header_tl
   {
    \# & \textbf{Student~Name} & \textbf{Signature}
   }
  % check the number
  \int_compare:nT { \seq_count:N \l_azetina_studentlist_seq > 20 }
   {
    \__azetina_studentlist_halve:
   }
  % make the table
  \par\noindent
  \begin{tabular}{|*{2}{r|l|w{c}{6em}|}}
  \hline
  \tl_use:N \l__azetina_studentlist_header_tl \\ \hline
  \seq_map_function:NN \l_azetina_studentlist_seq \__azetina_studentlist_row:n
  \end{tabular}
 }

\cs_new_protected:Nn \__azetina_studentlist_halve:
 {
  % make it even
  \int_if_odd:nT { \seq_count:N \l_azetina_studentlist_seq }
   {
    \seq_put_right:Nn \l_azetina_studentlist_seq { \multicolumn{3}{c|}{} }
   }
  % split in half
  \seq_clear:N \l__azetina_studentlist_temp_seq
  \int_step_inline:nn { \seq_count:N \l_azetina_studentlist_seq / 2 }
   {
    \seq_put_right:Nx \l__azetina_studentlist_temp_seq
     {
      \seq_item:Nn \l_azetina_studentlist_seq { ##1 } &
      \seq_item:Nn \l_azetina_studentlist_seq { ##1 + \seq_count:N \l_azetina_studentlist_seq / 2 }
     }
   }
  % replace the sequence
  \seq_set_eq:NN \l_azetina_studentlist_seq \l__azetina_studentlist_temp_seq
  % double the header
  \tl_put_right:Nx \l__azetina_studentlist_header_tl
   {
    & \exp_not:V \l__azetina_studentlist_header_tl
   }
 }

\cs_new_protected:Nn \__azetina_studentlist_row:n
 {
  #1 \\ \hline
 }

\ExplSyntaxOff

\begin{document}

\studentlist{namelist40}

\bigskip

\studentlist{namelist31}

\bigskip

\studentlist{namelist18}

\end{document}

这是文件namelist40.tex,其他两个文件只使用第一个n行。名称由https://homepage.net/name_generator/(如果人们发现自己被列入名单,请原谅)。

Alsop, Christian
Arnold, Carol
Bailey, Brian
Bell, Luke
Blake, Wendy
Bond, Abigail
Bower, Phil
Chapman, Austin
Cornish, Sebastian
Davidson, Sebastian
Dyer, Frank
Dyer, Molly
Grant, Harry
Henderson, Anna
Henderson, Rebecca
Henderson, Theresa
Howard, Joanne
Hudson, Peter
Ince, Brian
Kelly, Richard
Knox, Thomas
Lawrence, Isaac
MacLeod, Joe
McLean, John
Morrison, Liam
Murray, Stephen
Oliver, Stephanie
Payne, Sebastian
Powell, Kimberly
Pullman, Keith
Rampling, Faith
Rees, Joshua
Ross, Sebastian
Rutherford, Joanne
Sanderson, Hannah
Short, Kylie
Slater, Joshua
Springer, Jake
Springer, Sally
Sutherland, Lillian

在此处输入图片描述

相关内容