Short version: I am defining an environment:

\newenvironment{myenv}{\begin{trivlist}\item}{HERE something\end{trivlist}}

Now at the point marked with "HERE", I need a "conditional newline" doing the following:

If we are within a paragraph, emit a \newline. If we are not within a paragraph, e.g. because the content of the environment ends with displayed math, do not emit a newline.



should become

\begin{trivlist}\item Text\newline something\end{trivlist}


\begin{myenv}\item \[ xy \]\end{myenv}

should become

\begin{trivlist}\item \[ xy \]something\end{trivlist}

How can I achieve that?

Background: I need to draw a horizontal line at the end of the environment, and the vertical space between the content and the line is either too small for normal text or too large for displayed math.

Original problem, as requested in the comments:

}{%%% HERE should be the conditional newline
{\begin{tikzpicture}[remember picture,overlay]%
  \draw [solid,magenta,line width=2pt]
      \p2 =($(current page.west)$)
      (\x2,0) -- ($(\x2,0) + (10cm,0)$);
}% end newenvironment

The problem is that the distance between the line and the text either gets too small or to large, as LaTeX starts a new paragraph if the content ends with displayed math.

  • I think that \newline is the wrong tool anyway. – egreg Jun 23 '16 at 10:30
  • \ifvmode or \ifhmode may come in handy. But probably just using \par HERE is enough, and no need to test for anything. – Manuel Jun 23 '16 at 10:31
  • Please add the complete code of your environment \newenvironment{myenv}{.... and ask how to adjust the vertical space. – touhami Jun 23 '16 at 10:37
  • You never want to end a paragraph with \newline or \\ it produces a spurious white line (not vertical space) and an infinitely underful hbox warning in the log. – David Carlisle Jun 23 '16 at 10:43
  • Hm, conditional vspace would be fine as well. – JohnB Jun 23 '16 at 10:50

You can always check with \ifvmode or \ifhmode, e.g., \ifvmode do this\else do that\fi. But here you don't need that because \par acts that way (don't use \newline or \\ to end paragraphs). I added the \noindent which does no harm if \parskip=0pt and acts as you seem to want when \parskip≠0pt.

   \begin{tikzpicture}[remember picture,overlay]
     \draw [solid,magenta,line width=2pt]
         \p2 =($(current page.west)$)
         (\x2,0) -- ($(\x2,0) + (10cm,0)$);

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.