2

In my document it often happens that I ask a question, which I mark by making the text bold. I'd like to additionally place a 2-3mm grey rule to either the left-hand or the right-hand side of the text as shown in this example:

enter image description here

Is it possible to make this into an environment so I can merely write \question{....}? Currently I haven't decided whether I want this rule to the left or the right side of the question, but would like to see how this can be done.

Here is my MWE so far, which has everything beside the rule:

\documentclass[a5paper, 14pt]{memoir} \usepackage{lipsum} \begin{document} \chapter{One} \textbf{This is a question?} And here is the answer. And here is the answer. And here is the answer. And here is the answer. And here is the answer. \end{document} 
1
  • 2
    Check the tcolorbox package. Commented Oct 30, 2017 at 18:38

2 Answers 2

3

I suggest to use the tcolorbox package and apply a \newtcbox that draws a borderline west with some specified colour left to the text (which is 'west' in TikZ language).

If the rule should appear right, use borderline east. The first 3mm indicate the width of the rule, the second -3mm refer to a shifting to the left side.

\documentclass[a5paper, 14pt]{memoir} \usepackage{lipsum} \usepackage[most]{tcolorbox} \newtcbox{questionbox}[1][]{% enhanced, frame hidden, colback=white, fontupper=\bfseries, left=0pt, nobeforeafter, top=0pt, bottom=0pt, borderline west={3mm}{-3mm}{gray}, #1 } \begin{document} \chapter{One} \questionbox{This is a question?} And here is the answer. And here is the answer. And here is the answer. And here is the answer. And here is the answer. \end{document} 

enter image description here

Update

Using a tcolorbox environment in order to allow for line wrapping and breakable to break over pages.

\documentclass[a5paper, 14pt]{memoir} \usepackage{lipsum} \usepackage[most]{tcolorbox} \usepackage{blindtext} \newtcolorbox{questionbox}[1][]{% enhanced, frame hidden, colback=white, fontupper=\bfseries, left=0pt, nobeforeafter, top=0pt, bottom=0pt, borderline west={3mm}{-3mm}{gray}, breakable, #1 } \begin{document} \chapter{One} \begin{questionbox} This is a question \blindtext? \end{questionbox} And here is the answer. And here is the answer. And here is the answer. And here is the answer. And here is the answer. \end{document} 

enter image description here

7
  • I get the error I do not know the key '/tcb/borderline west',. Is my version of pgfplot too old? Commented Oct 30, 2017 at 18:50
  • @BillyJean: pgfplot seems unrelated. The tcolorbox version that adds the borderline feature was released in late October, 2014 Commented Oct 30, 2017 at 18:52
  • Thanks, this is nice. If the question is very long though, then it doesn't break the sentence but simply writes outside the page. Is it possible to break the question and extend the rule accordingly? Commented Oct 30, 2017 at 19:10
  • @BillyJean: Add breakable to the option list of the questionbox Commented Oct 30, 2017 at 20:21
  • 1
    @BillyJean: See the update at the end of my answer, please Commented Oct 30, 2017 at 21:19
1

A simple command using only the xcolor package, and an environment based on the leftbar environment from framed, which can break across pages:

\documentclass[a5paper, 14pt]{memoir} \usepackage{lipsum} \usepackage{framed} \usepackage[svgnames]{xcolor} \newcommand\myquestion[1]{\par\noindent\llap{\color{Gainsboro}\rule[-0.25ex]{0.5em}{2ex}\hspace{\marginparsep}}\textbf{#1}\par} \newenvironment{Question}{\par\noindent% \def\FrameCommand{\hspace{-1.1em}{\color{Gainsboro}\vrule width 0.5em}\hspace{0.6em}}% \MakeFramed {\advance\hsize-\width \FrameRestore}\bfseries\strut\hspace{-1.36\parindent}}% {\endMakeFramed\vspace{-\topsep}} \begin{document} \chapter{One} \myquestion{This is a question?} And here is the answer. And here is the answer. And here is the answer. And here is the answer. And here is the answer. \begin{Question} This is a very long question? Is this indeed a very long question? \end{Question} And here is a short answer. \end{document} 

enter image description here

5
  • This is also nice. This automatically breaks the sentence if it is too long, but doesn't extend the rule. Do I have to do that manually? Commented Oct 30, 2017 at 19:12
  • 1
    Ah! That is a different problem. I'll post another possibility in a moment. Commented Oct 30, 2017 at 19:16
  • Thanks for the edit, that is a nice solution. Is it possible to align the rule to the right of the sentence as well? Commented Oct 30, 2017 at 20:02
  • 1
    Do you mean two rules both on the left and the right side, or a single rule on the right? Commented Oct 30, 2017 at 20:06
  • Just a single rule to the right Commented Oct 30, 2017 at 20:10

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.