6

I want to know how to do a conditional line break. Whenever the error message "There is no line to end here" comes, I want to disable the line break, otherwise to enable it, with a \\ command or \newline command.

As suggested, I am now explaining below my problem, with a minimal sample tex file.

I am using two macros that allow me to "eat" a question or an answer, while allowing me to keep both of them together in the source. When I conduct an exam, I eat the answers and give only the questions, for example.

This example results in the following error message:

LaTeX Error: There's no line here to end.  

The reason is that I have the \\ [1ex] command at the beginning of the macro, \ans.

If I remove the \\ [1ex] command from the macro \ans, the error goes away. Unfortunately, however, the answer does not begin on a new line for the first question.

I would also like to disable the \\ [1ex] command when it is not required. For example, the behaviour is the same in the second question, whether or not this command is present. In fact, in some situations, this command could create unnecessary additional space.

\documentclass[12pt,a4paper]{article}
\newcommand{\quest}[1]{#1}
\newcommand{\eatquest}[0]{\renewcommand{\quest}[1]{}}
\newcommand{\ans}[1]{\\ [1ex] Answer: #1}
\newcommand{\eatans}[0]{\renewcommand{\ans}[1]{}}
\begin{document}
%\eatquest
%\eatans
\begin{enumerate}
\item
\quest{What is the effect of bad zeros?  How can it be overcome?
}
\ans{
 This results in poor performance of the system.  The bad zeros
  cannot be changed by feedback.  The only way to change them is to
  redesign the system (not controller) itself.
}
\item 
\quest{This question is on 2-DOF pole placement controller
  design. Answer the following: 
\begin{enumerate}
\item In the class, a method was proposed to handle unstable 2-DOF
  controllers.  This method also has unstable pole-zero cancellation.
  Why was this acceptable? 
\end{enumerate}
}
\ans{A short answer is given now:
\begin{enumerate}
\item The pole and zero come for the same, ``identical'', source.  In
  fact, one can even say that this is NOT a case of pole-zero
  cancellation. 
\end{enumerate}
}
\end{enumerate}
\end{document}
  • 4
    Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Apr 24 '14 at 6:00
  • 2
    By "line break" you mean \\, right? This command should be used very rarely, basically only in tabular-like environment and in multi-line displayed equations. New paragraph is started by an empty line in the code. – yo' Apr 24 '14 at 6:06
  • 4
    Use \par\addvspace{1ex} instead of \\[1ex] – egreg Apr 24 '14 at 7:33
  • 2
    Outside of tabular, try to avoid ever using \\ . – David Carlisle Apr 24 '14 at 8:40
2

Use \par\addvspace{1ex} instead of \\[1ex]

\documentclass[12pt,a4paper]{article}
\newcommand{\quest}[1]{#1}
\newcommand{\eatquest}[0]{\renewcommand{\quest}[1]{}}
\newcommand{\ans}[1]{\par\addvspace{1ex}Answer: #1}
\newcommand{\eatans}[0]{\renewcommand{\ans}[1]{}}
\begin{document}
%\eatquest
%\eatans
\begin{enumerate}
\item
\quest{What is the effect of bad zeros?  How can it be overcome?
}
\ans{
 This results in poor performance of the system.  The bad zeros
  cannot be changed by feedback.  The only way to change them is to
  redesign the system (not controller) itself.
}
\item 
\quest{This question is on 2-DOF pole placement controller
  design. Answer the following: 
\begin{enumerate}
\item In the class, a method was proposed to handle unstable 2-DOF
  controllers.  This method also has unstable pole-zero cancellation.
  Why was this acceptable? 
\end{enumerate}
}
\ans{A short answer is given now:
\begin{enumerate}
\item The pole and zero come for the same, ``identical'', source.  In
  fact, one can even say that this is NOT a case of pole-zero
  cancellation. 
\end{enumerate}
}
\end{enumerate}
\end{document}

enter image description here

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.