2

How to suppress the space at the beginning of very first para in the environment

MWE

\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}

enter image description here

enter image description here

Please clarify the below points

  1. In the above image, the first question, when i used \label{} in the environment the para is starting with little space, How to rectify or ignore the spaces at the begining

  2. when i am using the environment after the section title, the below space is coming more how to restrict the below space of \section

6
  • 1
    TeX interprets a line break after a } in the source code as space. Since you do not have a % behind the closing bracket of \label in the first Question tex renders a space. heading macros in TeX use the \@afterheading macro to suppress those spaces but they only work when there is a par boundary between the section and the following paragraph. If you add \@afterheading at the end of the begin-part of the definition (don't forget \makeatletter and \makeatother) and add an empty line at the beginning of the environment, you will notice that the space vanishes.
    – Lupino
    May 17 '18 at 10:11
  • @Lupino, is it possible to get automated way ?
    – vctu
    May 17 '18 at 10:15
  • i cant edit… replace \endgraf\noindent\ignorespaces} with \@afterindentfalse\@afterheading\par}
    – Lupino
    May 17 '18 at 10:22
  • the main reason is that you are using \noindent\textbf{Question~\thequest:} rather than using the latex commands to define headings or list items, both those constructs take care of this space and also take care of preventing a page break between the heading and the text. The usual way to declare these environments is using \newtheorem (which internally defines a one-item list with the heading as the list item) May 17 '18 at 10:25
  • @Lupino you should post that as the answer as that is the way to fix the current code (although using a real heading (that would apply \@afterheading automatically is another option:-) May 17 '18 at 10:27
5

To fix your example you need to mimic TeX's behaviour of a heading command sequence by adding \@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}
...

As David Carlisle pointed out, a better way would be to include the amsthm package and to use a theorem-like structure to define the environment:

\documentclass{book}
\usepackage{amsthm}
\newtheoremstyle{fakesec}{24pt}{24pt}{}{}{\bfseries}{}{\newline}{}
\theoremstyle{fakesec}
\newtheorem{question}{Question}
\begin{document}
  ...

Another way would be to replace the whole definition with something like

...
\newenvironment{question}
  {\refstepcounter{quest}%
   \subsection*{Question~\thequest:}}
  {\par\addvspace{24pt}}
...

Regarding your questions:

  1. TeX considers line breaks to be spaces and renders spaces that follow closing brackets into a word spacing in the output. To prevent that you need to comment out the line break after the argument of the label.

  2. heading macros in TeX have a rather complex mechanism which prevents indentation and spacing under certain circumstances. The two macros we added to your code in the first solution are a part of that mechanism.

0

I have two additional suggestions:

  • Make a habit of adding % (the comment symbol) immediately after each \label{...} instruction. Your code example already does so for question 2 (and that's why there's no unwanted indentation), but not for questions 1 and 3.

  • Redefine the question environment so that it takes a mandatory argument, viz., the label to be used for cross-referencing purposes. Since you're using \refstepcounter to increment the counter called quest, I'm assuming you intend to be able to cross-reference each and every question using the LaTeX \label-\ref mechanism. If that's the case, why not make the label argument mandatory -- one less thing to "forget" during editing...

The following shows the look that results from the second approach. Note that it's not necessary to terminate each and every line of code with a % symbol.

enter image description here

\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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.