2

I would like to span the list-item to textwidth automatically. If it's possible to align in 4 list to text-width (based on the long-length text) otherwise if it's to align in 3 list to text-width.

My MWE is:

\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}

My Expected Final Output is :

enter image description here

Balaji
  • 2,282
  • 1
    I don't think there's a way to do this automatically, but for horizontal lists in columns, you can use the tasks package. – Alan Munn Feb 07 '18 at 05:32
  • @Alan: I would expect horizontal lists in columns only and could you please provide sample code – Balaji Feb 07 '18 at 05:36
  • 1
    There's a complete example here: How to make horizontal lists? (effectively a duplicate, since automatic calculation is not possible). – Alan Munn Feb 07 '18 at 05:42
  • @AlanMunn I'm not so sure that automatic calculation is not possible. :-) I can imagine something like: (1) put each item in a \hbox and measure its natural width (2) do some computation with these widths to determine the number of columns (3) with the number of columns known, either defer to an existing environment like tasks, or typeset it yourself. – ShreevatsaR Feb 07 '18 at 06:01
  • @ShreevatsaR Well, probably not literally impossible, but be my guest... :) – Alan Munn Feb 07 '18 at 06:03
  • 1
    @AlanMunn Done :-) – ShreevatsaR Feb 07 '18 at 09:22

2 Answers2

2

If you know the number of columns you want (and don't need to compute it automatically), then you can use the tasks environment just specifying (3) or (4) respectively, as @AlanMunn suggested in the comments:

\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}

with tasks


If you want to the number of columns to be computed automatically, then it is more interesting. I'm not aware of an existing package that does this, but it could be implemented. All you'd have to do is:

  • put each item into a box,
  • use the widths of the boxes to compute the number of columns,
  • typeset the items to that number of columns.

Below is an implementation of this, done with LuaTeX for convenience (so compile the below with lualatex) although I believe all of this could be done with TeX macros too if you really wanted to. The typesetting can (and should) be improved, and even the code perhaps (right now a back-and-forth between TeX and Lua), but the automation is the interesting part (to me):

\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}

where autohlist.lua is:

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

with lua

ShreevatsaR
  • 45,428
  • 10
  • 117
  • 149
0

Here is a sample code with \usepackage[inline]{enumitem} for inline lists. I don't know the tasks package. It can be interesting to explore this too.

A solution to do what you want is to create minipages, with lists inside them, but you have to increase the width of the page.

\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}
Stan
  • 173