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}
2
  • \bf is deprecated: Use \textbf{...} or \bfseries instead (with proper grouping)
    – user31729
    Sep 15, 2015 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, 2015 at 21:25

2 Answers 2

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

4
  • 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, 2015 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, 2015 at 8: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, 2015 at 7: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, 2015 at 8:06
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}
3
  • This produces the desired result; however, I do have a couple of questions. First, would you mind explaining why my code wouldn't produce that second line? Second, and somewhat unrelated, why end lines with a comment character but no comment? Like at the end of your "\newcommand" line. I've seen that frequently around here.
    – Collin
    Sep 15, 2015 at 21:09
  • Using the sledgehammer…
    – egreg
    Sep 15, 2015 at 21:11
  • @C.Gonzalez: I've mentioned something about your first inquiry in my post. For the second question, see What is the use of percent signs (%) at the end of lines?
    – Werner
    Sep 15, 2015 at 21:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .