4

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}
Collin
  • 477
  • \bf is deprecated: Use \textbf{...} or \bfseries instead (with proper grouping) –  Sep 15 '15 at 20:45
  • Thanks for pointing that out! I'm new to LaTeX. I went ahead and made an edit so as not to distract from the central question. – Collin Sep 15 '15 at 21:25

2 Answers2

4

You need a "marker" at the beginning of the line in order for the leader \hrulefill to extend from. Try using \mbox{}\hrulefill for the ending rule. However, ...

The following will provide an unbreakable box (in the form of a tabular) that will insert your Q&A:

enter image description here

\documentclass{article}

\usepackage{tabularx}

\newcommand{\QandA}[2]{%
  \par\noindent
  \begin{tabularx}{\linewidth}{@{}lX@{}}
    \hline
    \textbf{Q:}& #1 \\
    \textbf{A:}& #2 \\
    \hline
  \end{tabularx}
  \par}

\begin{document}

Some text before.

\QandA{One}{Two}

Some text after.

\end{document}
Werner
  • 603,163
4

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

egreg
  • 1,121,712
  • When you say "no sledgehammer", you just mean that using the tabularx package is overkill for this case, correct? Also, how did you know that -1.25ex would provide symmetrical spacing? – Collin Sep 15 '15 at 23:14
  • @C.Gonzalez Yes, tabularx is definitely too much for this application. The value 1.25ex comes from an educated guess (I tried 1.2 and 1.3, then decided for 1.25). – egreg Sep 16 '15 at 08:14
  • Thanks! One other thing - what is meant by "glue"? I've seen it mentioned in other answers (also the term "penalties" appearing in conjunction). Let me know if this should be it's own question. – Collin Sep 17 '15 at 07:39
  • @C.Gonzalez “Glue” is the term Knuth uses for describing horizontal and vertical spacing that, in TeX, can be flexible (and disappear at appropriate spots, such as page and line breaks). For penalties, see What are penalties and which ones are defined?. – egreg Sep 17 '15 at 08:06