10

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:)

\documentclass{article}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{tikz}\usetikzlibrary{shapes.misc}
\newcommand\qnhead{%
\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,
            rectangle,
            minimum height=3.5ex] at (3cm,0ex) {
    \textnormal{\textbf{Question \thesection}}
};
}}
\titleformat{\section}{\itshape\large}{\qnhead}{0.1cm}{}
\renewcommand{\thesection}{\arabic{section}}

\begin{document}
\section{The text of the first question, which spans over more than %
         one line, will be placed here.}
\lipsum[2]
\section{The second question text will be placed here.}
\lipsum[2]
\end{document}

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
7

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:

\documentclass{article}
\usepackage{amsthm}
\usepackage[framemethod=TikZ]{mdframed}
\usepackage{lipsum}

\newlength\Questionwd
\setlength\Questionwd{2.8cm}
\newlength\Innerlsep
\setlength\Innerlsep{3pt}

% definition of the theorem style
\newtheoremstyle{mystyle}
  {\topsep}{\topsep}
  {\large\itshape%
    \parshape 2 0cm \linewidth \dimexpr\Questionwd-3pt\relax\dimexpr\linewidth-\Questionwd+3pt\relax%
  }
  {}{\bfseries}{}{0em}
  {\makebox[\dimexpr\Questionwd-\Innerlsep\relax][l]{%
    \thmname{#1}~\thmnumber{#2}\hfill}%
  }

% we use the previously defined style for the definition of the new theorem environment
% built with the mdframed package
\theoremstyle{mystyle}
\newmdtheoremenv[
  leftmargin=-\Questionwd,
  rightmargin=-8pt,
  innerleftmargin=\Innerlsep,
  linecolor=black!12,
  backgroundcolor=black!12,
  skipabove=0.65cm,
  skipbelow=0.20cm
]{question}{Question}

\begin{document}

\begin{question}
The text of the first question, which spans over more than one line, will be placed here.
\end{question}
\lipsum[2]
\begin{question}
The second question text will be placed here.
\end{question}
\lipsum[2]

\end{document}

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.