13

LaTeX has quite good facilities for making tables for technical writing. But what about for educational materials? The needs are very different, and I've never seen any discussion of this need or recommendation of suitable packages.

The document excerpt below is a nice simple example. This is not a table for displaying information; it is a table for students to write in. And after fiddling with LaTeX for some time I decided instead to use a word processor, take a screenshot, and include an image in the LaTeX file. I'd much rather be creating the table in LaTeX if possible.

Screenshot of table in context

So why was it easier to do this in a word processor?

  • Easy control over the size of each cell, which is important when students will be writing in the table.
  • Easy alignment, in this case centering horizontally and vertically.
  • Easy shading of selected cells.

I don't think environments like tabular can be coerced into handling this use case gracefully, so my question is this:

  • Is there a LaTeX package that allows for the simple creation of simple tables such as the one above?
4
  • 1
    Welcome to Text.Se. do you have a question? This is a Q&A site
    – Roland
    Commented Sep 4, 2021 at 7:47
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Sep 4, 2021 at 7:47
  • Edited to make the question explicit. Happy to edit again if necessary.
    – nosedog
    Commented Sep 4, 2021 at 7:52
  • I cannot see your image, but maybe you could try my tabularray package.
    – L.J.R.
    Commented Sep 4, 2021 at 7:58

6 Answers 6

12

An alternative solution with tblr environment of tabularray package:

\documentclass{article}

\usepackage{xcolor}
\usepackage{tabularray}

\begin{document}

\begingroup
\mathversion{bold}
\noindent
$\begin{tblr}{
  colspec = {*{8}{X[c]}},
  hlines, vlines,
  column{1} = {blue9},
  row{1} = {blue9},
  row{2-Z} = {1.2cm}, % Z stands for the last
}
      & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\
  2^n &   &   &   &   &   &   &   \\
  3^n &   &   &   &   &   &   &   \\
  4^n &   &   &   &   &   &   &   \\
  5^n &   &   &   &   &   &   &   \\
  6^n &   &   &   &   &   &   &   \\
\end{tblr}$
\endgroup

\end{document}

enter image description here

4
  • Thank you for this, L.J.R. The tabularray package looks excellent and I'm very glad to have now heard of it.
    – nosedog
    Commented Sep 5, 2021 at 1:56
  • I've accepted this answer. tabularray has worked beautifully for me, especially the ability to create a custom table environment with all the settings in one place, then easily create several like tables in the document. Thanks for your work on this library!
    – nosedog
    Commented Sep 8, 2021 at 7:44
  • @L.J.R. Can you please explain the * from the {*{8}{X[c]}},? I'm learning tabularray and I find it very useful for my needs. From what I understand 8=col no., X=p{some width, [c=center the column], but I don't understand *. The array package doc in § 4.2 is beyond my understanding. Thanks!
    – Mafsi
    Commented Nov 13, 2021 at 6:45
  • 1
    @Mafsi * means repeating several times. Therefore *{3}{lr} is the same as lrlrlr, and *{8}{X[c]} is the same as X[c]X[c]X[c]X[c]X[c]X[c]X[c]X[c].
    – L.J.R.
    Commented Nov 14, 2021 at 0:49
17

Something like this?

\documentclass[12pt]{article}
\usepackage[table, svgnames]{xcolor}
\usepackage{tabularx}
\setlength{\extrarowheight}{2pt}

\begin{document}

\begin{tabularx}{\textwidth}{|>{\columncolor{LightBlue!30}\rule[-0.4\hsize]{0pt}{\hsize}\centering\boldmath$}X<{$}| *{7}{X|}}
\multicolumn{8}{@{}p{\textwidth}@{}}{\textbf{Question 2 } Complete each of the tables below $\pmod 7$} \\[2ex] \hline
\rowcolor{LightBlue!30}\multicolumn{1}{|c|}{} & \thead{0} & \thead{1} & \thead{2} & \thead{3} & \thead{4} & \thead{5} & \thead{6} \\ \hline
2^n & & & & & & & \\ \hline
3^n & & & & & & & \\ \hline
4^n & & & & & & & \\ \hline
5^n & & & & & & & \\ \hline
6^n & & & & & & & \\ \hline
\end{tabularx}

\end{document}

enter image description here

5
  • And the centering of the title contents (the numbers)?
    – JoonasD6
    Commented Sep 4, 2021 at 18:57
  • 2
    @JoonasD6: I forgot this detail, but it's easy to fix. I'll do it in a moment.
    – Bernard
    Commented Sep 4, 2021 at 19:14
  • Lookin' good now! d(^_^) Since the powers are centered too, maybe just apply centering automatically to all columns? That might make the code neater.
    – JoonasD6
    Commented Sep 4, 2021 at 19:35
  • I just wondered whether there might be other cells not necessarily centred or boldfaced in the real use, that's why I used \thead, which ensures both for the head row (and even vertically centred).
    – Bernard
    Commented Sep 4, 2021 at 19:42
  • Thank you Bernard (and JoonasD6). The nicematrix approach looks more suitable, but it will be good for me to learn more about what I can do with that daily workhorse tabularx. Regarding real use, it's exactly as I presented, but of course situations vary. In fact, the real source document had several similar tables of various sizes, so an easy way to create them is very important.
    – nosedog
    Commented Sep 4, 2021 at 23:01
15

With {NiceTabular} of nicematrix.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\mathversion{bold}
\renewcommand{\arraystretch}{1.4}
$\begin{NiceArray}{*{8}{c}}[hvlines,columns-width=5mm]
\CodeBefore
  \rowcolor{lightgray}{1}
  \columncolor{lightgray}{1}
\Body
    & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\
    2^n \\
    3^n \\
    4^n \\
    5^n \\
    6^n 
\end{NiceArray}$

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

2
  • That execution seems to have a surprisingly easy way to modify the column width. That's the one thing that many tabular-based applications seem to be very cumbersome with.
    – JoonasD6
    Commented Sep 4, 2021 at 19:36
  • This looks like a wonderful option. Thank you! I'll review with a view to accept.
    – nosedog
    Commented Sep 4, 2021 at 22:55
8

The more you want to decorate the table (in particular for educational purpose, with colors, arrows, etc), the more TikZ's library matrix fits your need. For your requirement of simplicity, it is subjective!

enter image description here

\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\mathversion{bold} % swich to bold maths

\matrix[matrix of math nodes,nodes in empty cells,
row sep=-\pgflinewidth,column sep=-\pgflinewidth,
nodes={draw,minimum size=10mm,anchor=center,inner sep=0,outer sep=0},
column 1/.style={nodes={fill=green!20}},
row 1/.style={nodes={minimum height=6mm,fill=green!10}}
]{
&0&1&2&3&4&5&6\\
2^n&&&&&&&\\
3^n&&&&&&&\\
4^n&&&&&&&\\
5^n&&&&&&&\\
6^n&&&&&&&\\
};

\mathversion{normal} % swich back to normal maths
\end{tikzpicture}
\end{document}
2
  • 1
    Thank you Black Mild. I suspected that TikZ would be helpful and perhaps even necessary, but had no confidence to work it out myself. The code above is reasonably compact but still a bit too low-level for quick and easy use. But I will try it out and try to learn some lessons from it.
    – nosedog
    Commented Sep 5, 2021 at 2:28
  • @nosedog oh, come on! any code is just a piece of keywords, and there are not so many of them ^^ This is over 1000 pages, but in fact 100 pages is enough for usual drawing ctan.org/pkg/pgf
    – Black Mild
    Commented Sep 5, 2021 at 3:00
3

I've accepted L.J.R.'s answer and thought I'd leave an answer here with example code showing how I ultimately achieved what I wanted. I've stripped away most context and just focused on what I wanted to achieve: easy creation of suitable tables. Importantly, the last one, being larger, has narrower columns, and this setting is easily made on just that one table without having to set the width individually for every table.

\documentclass[10pt,a4paper]{article}

\usepackage[dvipsnames]{xcolor}
\usepackage{tabularray}
\usepackage[margin=0.8in]{geometry}
\usepackage{multicol}
\usepackage{parskip}

\setlength\columnsep{30pt}

\newcommand{\question}[2]{
  \textbf{Question\enspace#1}\quad#2
}

% Create a 'modtable' environment so that it is easy to
% include several like tables.

\NewTblrEnviron{modtable}
\SetTblrInner[modtable]{
  colspec   = {*{99}{X[c]}},   % 99 columns - we don't need to use them all
  hlines, vlines,              % all horizontal and vertical lines visible
  columns   = {8mm},           % set width for all columns...
  column{1} = {7mm},           % ...but make the first one difference
  row{1}    = {6mm},           % set height for first row...
  row{2-Z}  = {8mm},           % ...and height for the rest
  column{1} = {cyan9!30},      % background colour for first column and row
  row{1}    = {cyan9!30},
  colsep    = 0pt,             % no row or column separation
  rowsep    = 0pt,             %   (it interferes with setting precise width/height)
} 

\begin{document}

\begin{multicols}{2}
    
  \question{1}{Complete each of the tables below.}

  \medskip
  {\small(mod 7)}

  \begingroup
  \mathversion{bold}
  \quad
  $\begin{modtable}{}
        & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\
    2^n \\
    3^n \\
    4^n \\
    5^n \\
    6^n \\
  \end{modtable}$
  \endgroup

  \medskip
  {\small(mod 5)}

  \begingroup
  \mathversion{bold}
  \quad
  $\begin{modtable}{}
        & 0 & 1 & 2 & 3 & 4 \\
    2^n \\
    3^n \\
    4^n \\
  \end{modtable}$
  \endgroup

  \medskip
  {\small(mod 6)}

  \begingroup
  \mathversion{bold}
  \quad
  $\begin{modtable}{}
        & 0 & 1 & 2 & 3 & 4 & 5 \\
    2^n \\
    3^n \\
    4^n \\
    5^n \\
  \end{modtable}$
  \endgroup

  \columnbreak

  \question{2}{Complete each of the tables below.}

  \medskip
  {\small(mod 8)}

  \begingroup
  \mathversion{bold}
  \quad
  $\begin{modtable}{}
        & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 \\
    2^n \\
    3^n \\
    4^n \\
    5^n \\
    6^n \\
    7^n \\
  \end{modtable}$
  \endgroup

  \bigskip
  {\small(mod 10)}

  \begingroup
  \mathversion{bold}
  \quad
  $\begin{modtable}{ column{2-Z} = {7mm} }
        & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\
    2^n \\
    3^n \\
    4^n \\
    5^n \\
    6^n \\
    7^n \\
    8^n \\
    9^n \\
  \end{modtable}$
  \endgroup

\end{multicols}

\end{document}

Image of PDF rendering of above code

2

You can also try this: Table Generator to produce:

\documentclass{article}
\usepackage[table,xcdraw]{xcolor}
% If you use beamer only pass "xcolor=table" option, i.e. \documentclass[xcolor=table]{beamer}
\begin{document}
\begin{table}[]
\centering
\caption{Question 2 Complete Each of the Tables Below (mod 7)}
\label{tab:my-table}
\begin{tabular}{|l|l|l|l|l|l|l|l|}
\hline
\rowcolor[HTML]{DAE8FC} 
\multicolumn{1}{|c|}{\cellcolor[HTML]{DAE8FC}\textbf{}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}\textbf{0}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}\textbf{1}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}\textbf{2}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}\textbf{3}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}\textbf{4}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}\textbf{5}} & \multicolumn{1}{c|}{\cellcolor[HTML]{DAE8FC}\textbf{6}} \\ \hline
\cellcolor[HTML]{DAE8FC}$2^n$ &  &  &  &  &  &  &  \\ \hline
\cellcolor[HTML]{DAE8FC}$3^n$ &  &  &  &  &  &  &  \\ \hline
\cellcolor[HTML]{DAE8FC}$4^n$ &  &  &  &  &  &  &  \\ \hline
\cellcolor[HTML]{DAE8FC}$5^n$ &  &  &  &  &  &  &  \\ \hline
\cellcolor[HTML]{DAE8FC}$6^n$ &  &  &  &  &  &  &  \\ \hline
\end{tabular}
\end{table}
\end{document}
3
  • I failed to find if the generator can demand certain column widths. Did I miss this function or is it not there?
    – JoonasD6
    Commented Sep 4, 2021 at 19:36
  • @quintablet In the code, sure, but in the generator features?
    – JoonasD6
    Commented Sep 4, 2021 at 20:55
  • 1
    It's not perfect, but it let's me prototype tables in a rapid manner. I can further edit them to my satisfaction. For someone just learning LaTeX, it can be helpful ... it was for me.
    – quintablet
    Commented Sep 4, 2021 at 20:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .