我为我的数学书项目创建了两个宏。一个是枚举环境,它将问题编号为“示例 1.1”等等。另一个是将每个问题的元数据写入文本文件的命令。
问题是,由于某种原因,当我组合这两个宏时,出现了不必要的缩进,我不知道如何消除它。
\documentclass{article}
\newcounter{myenumi}
\renewcommand{\themyenumi}{\textbf{Example \thesection.\arabic{myenumi}.}}
\newenvironment{myenumerate}{%
% stuff for beginning of environment goes here
\setlength{\parindent}{0pt}% don't indent paragraphs
\setcounter{myenumi}{0}% restart numbering
\bigskip% skip a line
\renewcommand{\item}{% new definition of item
\par% start a new line
\medskip
\refstepcounter{myenumi}% advance counter
\makebox[8em][l]{\themyenumi}% print counter to width of 3em, aligned to left
}% end of definition of item
}{% at end of environment
\par% start new paragraph
\bigskip% skip a line
\noindent% don't indent new paragraph
\ignorespacesafterend% ignore spaces after environment
}
\makeatletter
\newwrite\@datawrite
\immediate\openout\@datawrite=\jobname.data
\newcommand\exampledata[3]{%
\immediate\write\@datawrite{Example \thesection.\theenumi}
\immediate\write\@datawrite{Source: #1}
\immediate\write\@datawrite{Checked: #2}
\immediate\write\@datawrite{Difficulty: #3}
\immediate\write\@datawrite{}}
\AtEndDocument{\closeout\@datawrite}
\makeatother
\begin{document}
\section{A section}
\begin{myenumerate}
\item
\exampledata{A nice book}{yes}{easy}
Show that $a=b$.
\item
\exampledata{Another nice book}{no}{impossible}
Show that $c=d$.
\end{myenumerate}
\begin{enumerate}
\item
\exampledata{A nice book}{yes}{easy}
Show that $a=b$.
\item
\exampledata{Another nice book}{no}{impossible}
Show that $c=d$.
\end{enumerate}
\end{document}
问题是,对于第一项,示例 1.1 和示例 1.2,问题陈述之前有一个我不想要的缩进。有人能帮我把它去掉吗?
提前感谢你的帮助。这是上述文档的图片。
答案1
\write{...}
你的错误是宏后面的空格\exampledata
。你需要修正你的宏:
\newcommand\exampledata[3]{%
\immediate\write\@datawrite{Example \thesection.\theenumi}%
\immediate\write\@datawrite{Source: #1}%
\immediate\write\@datawrite{Checked: #2}%
\immediate\write\@datawrite{Difficulty: #3}%
\immediate\write\@datawrite{}%
}
你需要使用
\exampledata{A nice book}{yes}{easy}%
反而
\exampledata{A nice book}{yes}{easy}
或者您需要\ignorespaces
在宏的末尾添加。
答案2
\themyenumi
您可以简单地减少定义环境时包含的框的长度myenumerate
。
将其从8em
减少到5.5em
:
\makebox[5.5em][l]{\themyenumi}
完整代码:
\documentclass{article}
\newcounter{myenumi}
\renewcommand{\themyenumi}{\textbf{Example \thesection.\arabic{myenumi}.}}
\newenvironment{myenumerate}{%
% stuff for beginning of environment goes here
\setlength{\parindent}{0pt}% don't indent paragraphs
\setcounter{myenumi}{0}% restart numbering
\bigskip% skip a line
\renewcommand{\item}{% new definition of item
\par% start a new line
\medskip
\refstepcounter{myenumi}% advance counter
\makebox[5.5em][l]{\themyenumi}% print counter to width of 3em, aligned to left
}% end of definition of item
}{% at end of environment
\par% start new paragraph
\bigskip% skip a line
\noindent% don't indent new paragraph
\ignorespacesafterend% ignore spaces after environment
}
\makeatletter
\newwrite\@datawrite
\immediate\openout\@datawrite=\jobname.data
\newcommand\exampledata[3]{%
\immediate\write\@datawrite{Example \thesection.\theenumi}
\immediate\write\@datawrite{Source: #1}
\immediate\write\@datawrite{Checked: #2}
\immediate\write\@datawrite{Difficulty: #3}
\immediate\write\@datawrite{}}
\AtEndDocument{\closeout\@datawrite}
\makeatother
\begin{document}
\section{A section}
\begin{myenumerate}
\item
\exampledata{A nice book}{yes}{easy}
Show that $a=b$.
\item
\exampledata{Another nice book}{no}{impossible}
Show that $c=d$.
\end{myenumerate}
\begin{enumerate}
\item
\exampledata{A nice book}{yes}{easy}
Show that $a=b$.
\item
\exampledata{Another nice book}{no}{impossible}
Show that $c=d$.
\end{enumerate}
\end{document}