[Tex/LaTex] \hrulefill not working as expected in \newcommand

rules

I'm trying to make a simple new command for that creates a break in the text for a "Question and Answer" block preceded and succeeded by a horizontal line.

With the existing code (listed below) the bottom horizontal line does not show.

\newcommand{\QandA}[2]{
    \hrulefill \\
    \textbf{Q:  }{#1} \\
    \textbf{A:  }{#2} \\
    \hrulefill}

Best Answer

No sledgehammer like tabularx; the important thing is to avoid \hrulefill at the start of a line, because glue disappears at line breaks. The first is after the indentation box or at the beginning of the paragraph like in my answer, but it doesn't disappear there. For the second one we need something that will not vanish at a line break. like \mbox{}.

\documentclass{article}

\newcommand{\QandA}[2]{%
  \par\noindent\hrulefill\\*
  \textbf{Q: }#1\\*
  \textbf{A: }#2\\*[-1.25ex]
  \mbox{}\hrulefill\par
}

\begin{document}

Some text before.

\QandA{One}{Two}

Some text after.

\end{document}

The use of * is in order to avoid page breaks.

enter image description here