我想要在 tabularx 里面有手写线条,所以我写了这个:
\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}
不幸的是我得到了这个错误
! Missing \endgroup inserted.
<inserted text>
\endgroup
l.27 \end{tabularx}
任何想法?
答案1
我相信您想用线条填充空间并为问题留出更多空间。
\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}
\notes[20pt]{3}
如果您想在行间留出更多空间,您仍然可以呼叫。
答案2
正如评论中已经提到的,您不能\\
在这里使用 来开始新行。因此,我已将 替换为您定义中的。我还略微更改了一些命令的\\
顺序,以便在第一行上方留出更多空间。最后,我还添加了一个使用并将命令放在类型列中的示例以及一个带有类型列的示例:\newline
\notes
tabular
\notes
p
tabularx
X
\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}