列表项自动跨越至文本宽度

列表项自动跨越至文本宽度

我想自动将列表项扩展到文本宽度。如果可以的话,可以按照 4 个列表对齐到文本宽度(基于长文本),否则,按照 3 个列表对齐到文本宽度。

我的 MWE 是:

\documentclass{book}
\usepackage{amsmath}
\usepackage{enumitem}
\setcounter{secnumdepth}{0}

\begin{document}

\section{Digital Resources for this chapter}

In the Interactive Textbook:
\begin{itemize}
\item Videos
\item Literacy worksheet
\item Quick Quiz
\item Solutions (enabled by teacher)
\item Widgets
\item Study guide
\end{itemize}

In the Online Teaching Suite:
\begin{itemize}
\item Teaching Program
\item Tests
\item Review Quiz
\item Teaching Notes
\end{itemize}

\end{document}

我预期的最终输出是:

在此处输入图片描述

答案1

如果您知道所需的列数(并且不需要自动计算),那么您可以使用tasks仅指定(3)(4)分别指定的环境,正如@AlanMunn 在评论中所建议的那样:

\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage{tasks}
\begin{document}

In the Interactive Textbook:
\begin{tasks}[style=itemize](3)
\task Videos
\task Literacy worksheet
\task Quick Quiz
\task Solutions (enabled by teacher)
\task Widgets
\task Study guide
\end{tasks}

In the Online Teaching Suite:
\begin{tasks}[style=itemize](4)
\task Teaching Program
\task Tests
\task Review Quiz
\task Teaching Notes
\end{tasks}

\end{document}

有任务


如果您希望自动计算列数,那么这更有趣。我不知道现有的包可以做到这一点,但它可以实现。您所要做的就是:

  • 把每件物品放入一个盒子里,
  • 使用框的宽度来计算列数,
  • 将项目排版为该列数。

下面是一个实现,为了方便使用 LuaTeX(因此使用 编译以下内容lualatex),但我相信如果您真的想要,所有这些都可以使用 TeX 宏来完成。排版可以(也应该)改进,甚至代码也可能会改进(目前在 TeX 和 Lua 之间来回切换),但自动化才是最有趣的部分(对我来说):

\documentclass{article}
\usepackage[margin=1in]{geometry}
\begin{document}

\directlua{dofile('autohlist.lua')}
\def\litem#1{\directlua{add_item([[{\textbullet} #1]])}}

\newenvironment{autohlist}{%
    \par%
    \directlua{clear_items()}%
    \vskip\baselineskip%
  }{%
    \directlua{print_items()}%
    \directlua{clear_items()}%
  }

In the Interactive Textbook:
\begin{autohlist}
\litem {Videos}
\litem {Literacy worksheet}
\litem {Quick Quiz}
\litem {Solutions (enabled by teacher)}
\litem {Widgets}
\litem {Study guide}
\end{autohlist}

In the Online Teaching Suite:
\begin{autohlist}
\litem {Teaching Program}
\litem {Tests}
\litem {Review Quiz}
\litem {Teaching Notes}
\end{autohlist}

\end{document}

哪里autohlist.lua

print('')
local boxnum = 0
local prefix = 'tmpboxnameprefix'
function newname()
   boxnum = boxnum + 1
   local n = boxnum
   local s = ''
   while n > 0 do
      local rem = n % 26
      n = (n - rem) / 26
      s = s .. string.char(string.byte('a') + rem)
   end
   return s
end

local item_names = {}
function add_item(contents)
   local s = string.format('%s%s', prefix, newname())
   tex.print(string.format(
                [[\newbox\%s\setbox\%s\hbox{%s}]],
                s, s, contents))
   table.insert(item_names, s)
end

function print_items()
   local max_width = 0
   for i, name in ipairs(item_names) do
      max_width = math.max(max_width, tex.getbox(name).width)
   end
   local num_columns = math.floor(tex.get('hsize') / max_width)
   num_columns = math.min(num_columns, #item_names)
   print(string.format('Number of columns is %s', num_columns))
   local each_width = tex.get('hsize') / num_columns
   local num_rows = math.ceil(#item_names / num_columns)
   local i = 1
   for r = 1, num_rows do
      tex.print([[\hbox{%%]])
      for c = 1, num_columns do
         if i <= #item_names then
            tex.print(string.format(
                         [[\hbox to \dimexpr(\hsize / %s){\unhbox\%s\hfil}%%]],
                         num_columns, item_names[i]))
            i = i + 1
         end
      end
      tex.print([[}]])
      tex.print([[\vskip\baselineskip]])
   end
end

function clear_items()
   for i = 1, #item_names do
      item_names[i] = nil
   end
end

使用 lua

答案2

这是内联列表的示例代码\usepackage[inline]{enumitem}。我不知道这个tasks包。探索这个也很有趣。

解决方案是创建包含列表的小页面,但您必须增加页面的宽度。

\documentclass{book}
\usepackage{amsmath}
\usepackage[inline]{enumitem}
\setcounter{secnumdepth}{0}
\usepackage[letter,inner=30mm,outer=30mm]{geometry}

\begin{document}

\section{Digital Resources for this chapter}

In the Interactive Textbook:

\bigbreak 
\begin{itemize*}[itemjoin=\hspace{1cm}]
\item Videos
\item Literacy worksheet
\item Quick Quiz
\end{itemize*}

\medbreak 
\begin{itemize*}[itemjoin=\hspace{1cm}]
\item Solutions (enabled by teacher)
\item Widgets
\item Study guide
\end{itemize*}

\bigbreak 
In the Online Teaching Suite:

\medbreak 
\begin{itemize*}[itemjoin=\hspace{1cm}]
\item Teaching Program
\item Tests
\item Review Quiz
\item Teaching Notes
\end{itemize*}

\bigbreak 
\hrule
\bigbreak 



\section{Digital Resources for this chapter}

In the Interactive Textbook:


\bigbreak 
\noindent
\begin{minipage}[m]{5.5cm}
\begin{itemize}
\item Videos
\item Solutions (enabled by teacher)
\end{itemize}
\end{minipage}
\begin{minipage}[m]{4cm}
\begin{itemize}
\item Literacy worksheet
\item Widgets
\end{itemize}
\end{minipage}
\begin{minipage}[m]{3cm}
\begin{itemize}
\item Quick Quiz
\item Study guide
\end{itemize}
\end{minipage}
\end{document}

相关内容