7

I created two custom environment and a custom command to easily display an overview of modules and classes they contain in tabular format. However, I end up with unwanted indentation for the the first class name in each module. How can I remove this indentation?

Example:

\documentclass[12pt]{article}

\usepackage{multirow}
\usepackage{etoolbox}

% PREAMBLE

%
% ===================== %
% Module overview table %
% ===================== %
\newenvironment{moduleoverview}{%
    \begin{tabular}{|c|c|}
        \hline
            \textbf{Module} & 
            \textbf{Class}
        \\ \hline
}
{%
    \end{tabular}
}

\newtoggle{firstmtclass}
\newenvironment{momodule}[2]{%
    % First argument: module name
    % Second argument: number of classes in module
    \multirow{#2}{*}{%
        #1  
    }
    &   
    \toggletrue{firstmtclass}
}
{%
    \hline
}

\newcommand{\moclass}[1]{%
    % First argument: class name
    \iftoggle{firstmtclass}{%
        \togglefalse{firstmtclass}
    }{%
        &
    }
        #1
    \\
}

\begin{document}

\begin{moduleoverview}
    \momodule{Module name A}{2}
        \moclass{class name 1}
        \moclass{class name 2}
    \endmomodule
    \momodule{Module name B}{3}
        \moclass{class name 3}
        \moclass{class name 4}
        \moclass{class name 5}
    \endmomodule
\end{moduleoverview}

\end{document}

Produces:

enter image description here

5

You're missing some % and an \ignorespaces: the space (end-of-line) after

\momodule{Module name A}{2}

doesn't get ignored.

I reformatted the code, or I wouldn't have been able to understand it.

\documentclass[12pt]{article}

\usepackage{multirow}
\usepackage{etoolbox}

% PREAMBLE

%
% ===================== %
% Module overview table %
% ===================== %
\newenvironment{moduleoverview}
 {%
  \begin{tabular}{|c|c|}
  \hline
  \textbf{Module} & \textbf{Class} \\ \hline
 }
 {%
  \end{tabular}
 }

\newtoggle{firstmtclass}
\newenvironment{momodule}[2]
 {% First argument: module name
  % Second argument: number of classes in module
  \multirow{#2}{*}{#1} & \toggletrue{firstmtclass}\ignorespaces % <-- This was missing
 }
 {%
  \hline
 }

\newcommand{\moclass}[1]{%
  % First argument: class name
  \iftoggle{firstmtclass}
   {\togglefalse{firstmtclass}}
   { & }% <-- This was missing
   #1 \\
}

\begin{document}

\begin{moduleoverview}
  \momodule{Module name A}{2}
    \moclass{class name 1}
    \moclass{class name 2}
  \endmomodule
  \momodule{Module name B}{3}
    \moclass{class name 3}
    \moclass{class name 4}
    \moclass{class name 5}
  \endmomodule
\end{moduleoverview}

\end{document}

enter image description here

I propose a different solution, which uses LaTeX3 programming; the syntax is changed, but I believe it's easier to manage, as you don't need to pass the number of modules.

\documentclass[12pt]{article}

\usepackage{xparse}

\newenvironment{moduleoverview}
 {
  \begin{tabular}{|c|c|}
  \hline
  \textbf{Module} & \textbf{Class} \\ \hline
 }
 {
  \end{tabular}
 }

\ExplSyntaxOn
\NewDocumentCommand{\momodule}{ m m }
 {% First argument: module name
  % Second argument: modules
  #1 & \momodules_modules:n { #2 }
 }

\seq_new:N \l_momodules_modules_seq
\cs_new_protected:Npn \momodules_modules:n #1
 {
  \seq_set_split:Nnn \l_momodules_modules_seq { , } { #1 }
  \begin{tabular}{@{}c@{}}
  \seq_use:Nn \l_momodules_modules_seq { \\ }
  \end{tabular} \\ \hline
 }
\ExplSyntaxOff

\begin{document}

\begin{moduleoverview}
  \momodule{Module name A}{
    class name 1,
    class name 2
  }
  \momodule{Module name B}{
    class name 3,
    class name 4,
    class name 5
  }
\end{moduleoverview}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.