如何抑制环境中第一个段落开头的空格
平均能量损失
\documentclass{book}
\newcounter{quest}
\newenvironment{question}{\par\addvspace{24pt}\refstepcounter{quest}%
\noindent\textbf{Question~\thequest:}\endgraf
\noindent\ignorespaces}
{\par\addvspace{24pt}}
\begin{document}
\begin{question}\label{quest1}
Languages may use both syntax and prosody to distinguish interrogative sentences (which pose questions) from declarative sentences (which state propositions). Syntax refers to grammatical changes, such as moving words around or adding question words; prosody refers here to changes in intonation while speaking.
\end{question}
\begin{question}\label{quest1}%
Languages may use both syntax and prosody to distinguish interrogative sentences (which pose questions) from declarative sentences (which state propositions). Syntax refers to grammatical changes, such as moving words around or adding question words; prosody refers here to changes in intonation while speaking.
\end{question}
\section{Sample head}
\begin{question}\label{quest1}
Languages may use both syntax and prosody to distinguish interrogative sentences (which pose questions) from declarative sentences (which state propositions). Syntax refers to grammatical changes, such as moving words around or adding question words; prosody refers here to changes in intonation while speaking.
\end{question}
\section{Sample head}
Languages may use both syntax and prosody to distinguish interrogative sentences (which pose questions) from declarative sentences (which state propositions). Syntax refers to grammatical changes, such as moving words around or adding question words; prosody refers here to changes in intonation while speaking.
\end{document}
请澄清以下几点
在上图中,第一个问题是,当我在环境中使用 \label{} 时,段落开头的空格很少,如何纠正或忽略开头的空格
当我使用部分标题后的环境时,下面的空间越来越多,如何限制 \section 下面的空间
答案1
要修复您的示例,您需要通过添加以下内容来模仿 TeX 的标题命令序列行为\@afterindentfalse\@afterheading
:
\documentclass{book}
\newcounter{quest}
\makeatletter
\newenvironment{question}{\par\addvspace{24pt}\refstepcounter{quest}%
\noindent\textbf{Question~\thequest:}%
\@afterindentfalse\@afterheading\par}
{\par\addvspace{24pt}}
\makeatother
\begin{document}
...
正如 David Carlisle 指出的那样,更好的方法是包含该amsthm
包并使用类似定理的结构来定义环境:
\documentclass{book}
\usepackage{amsthm}
\newtheoremstyle{fakesec}{24pt}{24pt}{}{}{\bfseries}{}{\newline}{}
\theoremstyle{fakesec}
\newtheorem{question}{Question}
\begin{document}
...
另一种方法是用类似以下内容替换整个定义
...
\newenvironment{question}
{\refstepcounter{quest}%
\subsection*{Question~\thequest:}}
{\par\addvspace{24pt}}
...
关于您的问题:
TeX 将换行符视为空格,并将右括号后面的空格渲染为输出中的单词间距。为了防止这种情况,您需要注释掉标签参数后的换行符。
TeX 中的标题宏具有相当复杂的机制,可在某些情况下防止缩进和空格。我们在第一个解决方案中添加到您的代码中的两个宏是该机制的一部分。
答案2
我还有另外两个建议:
%
养成在每条指令后立即添加(注释符号)的习惯\label{...}
。您的代码示例已经针对问题 2 这样做了(这就是为什么没有不必要的缩进),但针对问题 1 和 3 却没有这样做。重新定义
question
环境,使其接受强制参数,即,label
用于交叉引用目的。由于您使用\refstepcounter
来增加名为 的计数器quest
,我假设您打算使用 LaTeX\label
-\ref
机制交叉引用每个问题。如果是这样,为什么不将标签参数设为强制性的——这样在编辑过程中就少了一件容易“忘记”的事情……
下面显示了第二种方法的结果。请注意,没有必要用符号终止每一行代码%
。
\documentclass{book}
\newcounter{quest}
\newenvironment{question}[1]{% % env. takes 1 argument
\par\addvspace{24pt}
\refstepcounter{quest}
\label{#1} % <-- new -- make sure this comes *after* '\refstepcounter'
\noindent\textbf{Question~\thequest:}\endgraf
\noindent\ignorespaces}{%
\par\addvspace{24pt}}
\begin{document}
\setcounter{chapter}{2} % just for this example
\begin{question}{quest1} % note that it's ok for the next line to start with whitespace
Languages may use both syntax and prosody to distinguish interrogative
sentences (which pose questions) from declarative sentences (which state
propositions). Syntax refers to grammatical changes, such as moving words
around or adding question words; prosody refers here to changes in intonation
while speaking.
\end{question}
\begin{question}{quest2}
Languages may use both syntax and prosody to distinguish interrogative
sentences (which pose questions) from declarative sentences (which state
propositions). Syntax refers to grammatical changes, such as moving words
around or adding question words; prosody refers here to changes in intonation
while speaking.
\end{question}
\section{Sample head}
\begin{question}{quest3}
Languages may use both syntax and prosody to distinguish interrogative
sentences (which pose questions) from declarative sentences (which state
propositions). Syntax refers to grammatical changes, such as moving words
around or adding question words; prosody refers here to changes in intonation
while speaking.
\end{question}
\end{document}