2

I was attempting to simplify my LaTeX document to write a Maths assessment with LaTeX, but when I created new environments I got an error that I cannot solve. Can you please explain me what is wrong with my definition? The source is the following:

\documentclass{article}
\usepackage{tabu}
\usepackage{longtable}

% ASSESSMENT TEXT
\newenvironment{assessment}
    {\tabulinesep=1.5mm
     \begin{longtabu} to \linewidth{|X[13 l]|X[c]|}
     \hline
      & \scriptsize DO NOT WRITE IN THIS MARGIN \cr}
    {\end{longtabu}}

% Pattern \begin{AssExer}{mark}{space}
\newcounter{exercise}
\newenvironment{AssExerc}[2]
    {\hline
     \begin{minipage}[t]{\linewidth}
     \refstepcounter{exercise}\textbf{\theexercise.}~}
    {\end{minipage}\vspace{#2} & \textbf{#1}\cr
     \hline}

\begin{document}
\begin{assessment}
\begin{AssExerc}{1}{2cm}
Solve the following equation $2x-1 = 0$.
\end{AssExerc}
\end{assessment}
\end{document}

The assessment environment works, but the AssExerc doesn't. Thank you for your help.

3
  • Welcome to TeX.SX!
    – Bobyandbob
    Commented Nov 4, 2017 at 12:24
  • Well, first of all, you need to provide an MWE (Minimum Working Example), so that we can try to compile your code and see the errors for ourselves. Also, maybe you just have a typo, if you copied this from your code: the \newenvironment{AssExer} is missing the c.
    – basseur
    Commented Nov 4, 2017 at 12:41
  • I copied only the definitions because the code is kind of a nightmare! I am copying now the source with a minimal working example. Perhaps it is not that minimal and I apologise for that, but I honestly do not know how to provide a minimal one.
    – Logos
    Commented Nov 4, 2017 at 12:54

1 Answer 1

2

There's a few problems with your code. First, you cannot use an argument to an environment in the \end{environment} code. We can get around this by putting the arguments into temporary global variables \gdef\temppoints{#1}\gdef\tempvskip{#2}.

The second more bothersome issue is that the environment starts a group and inside that group you cannot add elements to the table like & or \cr or \hline. To get around this, we can use \AfterEndEnvironment which adds code after the environment group is ended and is defined in the etoolbox package. We need to move & \textbf{#1}\cr\hline into \AfterEndEnvironment. I don't have a similar workaround for the \hline in the \begin{AssExer} part, but luckily that \hline is unneeded.

\documentclass{article}

\usepackage{tabu}
\usepackage{longtable}
\usepackage{etoolbox}

% ASSESSMENT TEXT
\newenvironment{assessment}
    {\tabulinesep=1.5mm
     \begin{longtabu} to \linewidth{|X[13 l]|X[c]|}
     \hline
      & \scriptsize DO NOT WRITE IN THIS MARGIN \cr\hline}
    {\end{longtabu}}

% Pattern \begin{AssExer}{mark}{space}
\newcounter{exercise}
\newenvironment{AssExer}[2]{
     \begin{minipage}[t]{\linewidth}
     \refstepcounter{exercise}\textbf{\theexercise.}~
     \gdef\temppoints{#1}\gdef\tempvspace{#2}
}{
     \end{minipage}\vspace{\tempvspace} 
}
\AfterEndEnvironment{AssExer}{& \textbf{\temppoints}\cr\hline}

\begin{document}

\begin{assessment}
\begin{AssExer}{1}{2cm}
Solve the following equation $2x-1 = 0$.
\end{AssExer}
\end{assessment}

\end{document} 
2
  • Thank you very much. It works perfectly now. I did not know of these packages! I realise my attempt was kind of naif!
    – Logos
    Commented Nov 4, 2017 at 13:28
  • Actually the \AfterEndEnviornment problem is nasty -- it's hard to fix that sort of problem. Commented Nov 4, 2017 at 13:36

You must log in to answer this question.

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