I'm using LaTeX to write up an assignment which comprises a number of questions. I'd like to break up the questions from my answers by placing the section headings inside a shaded box. I'm relatively new to LaTeX.

So far, from searching around and reading quite a few examples, I've managed to knock together something of a partial solution (attempted minimal working example here:)

\tikz[baseline,trim left=3.1cm, trim right=3cm] {
        \fill [black!12] (0.5cm,-1ex) rectangle (\textwidth+3.3cm,2.5ex);
        \node [
            anchor= base east,
            minimum height=3.5ex] at (3cm,0ex) {
    \textnormal{\textbf{Question \thesection}}

\section{The text of the first question, which spans over more than %
         one line, will be placed here.}
\section{The second question text will be placed here.}

This works great until I have a question that ends up spanning over multiple lines. It's a limitation, I think, of the way I've got tikz drawing the rectangle, but I can't work out how to modify the code to make it span multiple lines.

Does anyone have any suggestions as to how I would go about fixing this?

enter image description here

  • Why not just replace the whole \section command by a new version that typesets what you want using TikZ? Alternatively you could define a \question command which does this. – yori May 23 '12 at 3:21
  • I've edited my answer adding some improvements. – Gonzalo Medina May 23 '12 at 17:46
  • Similar Q&A: tex.stackexchange.com/q/34288/8272 – Nikos Alexandris Oct 14 '12 at 14:21

I propose a different approach: to use the mdframed and amsthm packages to define a theorem-like environment, so you don't have to change the meaning of \section. Something along the following lines:



% definition of the theorem style
    \parshape 2 0cm \linewidth \dimexpr\Questionwd-3pt\relax\dimexpr\linewidth-\Questionwd+3pt\relax%

% we use the previously defined style for the definition of the new theorem environment
% built with the mdframed package


The text of the first question, which spans over more than one line, will be placed here.
The second question text will be placed here.


enter image description here

  • the space below the shaded question box is a bit larger than that above the box, implying a visual association of the question box with what precedes rather than follows it. decreasing the space below would be a distinct improvement, although increasing the space above would probably be easier. otherwise, very nice. – barbara beeton May 23 '12 at 13:20
  • @barbarabeeton you're right. I've made some improvements (I hope) including the vertical spacing before and after the environment. – Gonzalo Medina May 23 '12 at 16:55

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.