# Insert \rule in \tabularx?

I would like to have lines for handwriting inside a tabularx, so I wrote this:

\documentclass[10pt]{article}
\usepackage[a4paper, total={18cm, 25cm}]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{fontspec}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{tabularx}
\usepackage{pgffor, ifthen}

\newcommand{\notes}[3][\empty]{%
\foreach \n in {1,...,#2}{%
\ifthenelse{\equal{#1}{\empty}}
{\rule{#3}{0.5pt}\\}
{\rule{#3}{0.5pt}\vspace{#1}\\}
}
}

\begin{document}

\begin{tabularx}{\textwidth}{|l|l|}
\hline
\textbf{Question} & \textbf{Answer} \\
\hline
foo & \notes[10pt]{3}{5cm}\\
\hline
\end{tabularx}

\end{document}


Unfortunately I get this error

! Missing \endgroup inserted.
<inserted text>
\endgroup
l.27 \end{tabularx}


Any idea?

• Why do you use tabularx if you don't have an X type column in there? – leandriis Jan 11 at 13:42
• You can not ues \\  inside of table cells in l type columns to start a new line. You might want to replace \\  in your \notes command with \newline and use the command inside of a p or similar column type. – leandriis Jan 11 at 13:47

## 2 Answers

I believe you want to fill out the space with the lines and also to give more room for the questions.

\documentclass[10pt]{article}
\usepackage[a4paper, total={18cm, 25cm}]{geometry}

\usepackage{tabularx,xparse}

\usepackage{lipsum}

\ExplSyntaxOn

\NewDocumentCommand{\notes}{O{10pt}m}
{
\prg_replicate:nn { #2-1 }
{
\raisebox{#1}{\strut}
\hrulefill\par
}
\raisebox{#1}{\strut}
\hrulefill
}

\ExplSyntaxOff

\begin{document}

\noindent
\begin{tabularx}{\textwidth}{|>{\hsize=0.5\hsize\raggedright}X|>{\hsize=1.5\hsize}X|}
\hline
\textbf{Question} & \textbf{Answer} \\
\hline
\lipsum[1][1] & \notes{3}\\
\hline
\hline
foo & \notes{1}\\
\hline
\end{tabularx}

\end{document}


You can still call \notes[20pt]{3} if you want to leave more space between the lines.

As already mentioned in a comment, you can't use \\ to sart a new line here. I have therefore replaced the \\ with \newline in your definition of \notes. I have also slightly changed the order of some commands in order to allow for more space above the first line. Lastly, I have also added an example using tabular and placing the \notes command in a p type column as well as a tabularx example with an X type column:

\documentclass[10pt]{article}
\usepackage[a4paper, total={18cm, 25cm}]{geometry}
\usepackage{fontspec}
\usepackage[french]{babel}
\usepackage{tabularx}
\usepackage{pgffor, ifthen}

\newcommand{\notes}[3][\empty]{%
\foreach \n in {1,...,#2}{%
\ifthenelse{\equal{#1}{\empty}}
{\rule{#3}{0.5pt}\newline}  %<---- replaced \\ with \newline here
{\hspace{0pt}\newline\rule{#3}{0.5pt}\vspace{#1}} % <---- removed  \\ at the end of the line and placed  \hspace{0pt}\newline in the beginning
}
}

\begin{document}
\noindent
\begin{tabular}{|l|p{5.5cm}|}
\hline
\textbf{Question} & \textbf{Answer} \\
\hline
foo & \notes[10pt]{3}{5cm}\\
\hline
foo & \notes[10pt]{1}{5cm}\\
\hline
\end{tabular}

\noindent
\begin{tabularx}{\textwidth}{|l|X|}
\hline
\textbf{Question} & \textbf{Answer} \\
\hline
foo & \notes[10pt]{3}{5cm}\\
\hline
foo & \notes[10pt]{1}{5cm}\\
\hline
\end{tabularx}
\end{document}