Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to typeset blocks of text in a multicolumn environment, with a title above, and framed with full line width.

I'm using the multicol package for that. The issue I face is that the block of text (in the multicols environment) appears to be narrower than the title.

According to the multicol documentation, it should work fine :

The space between columns is controlled by the length parameter \columnsep. The width for the individual columns is automatically calculated from this parameter and the current \linewidth.

However it doesn't :

\documentclass{article}

\usepackage{multicol}
\usepackage{lipsum}

\begin{document}

\noindent
\fbox{
  \begin{minipage}{1.0\linewidth}
    Title
  \end{minipage}
}

\begin{multicols}{2}
  \lipsum[1-5]
\end{multicols}

\end{document}

Output

share|improve this question
    
The tite box is wider for two reasons: a) you have a space after \fbox{ which ends up inside the frame and b) as pointed out the width of the \fbox is width of content + 2*\fboxsep, so you need to reduce the width of the minipage accordingly or use \framebox. –  Frank Mittelbach Oct 27 '12 at 6:58
add comment

2 Answers

up vote 6 down vote accepted

It's caused by the padding that fbox has, the easiest solution is to use framebox because it's possible to specify the width:

\documentclass{article}
\usepackage{multicol}
\usepackage{lipsum}
\begin{document}
\noindent
\framebox[\linewidth][l]{
    Title
}

\begin{multicols}{2}
  \lipsum[1-5]
\end{multicols}

\end{document}

Which results in the following:

Result

If for some reason you absolutely need the minipage, you can use this:

\documentclass{article}   
\usepackage{multicol}
\usepackage{lipsum}
\begin{document}
\noindent
\framebox[\linewidth]{
    \begin{minipage}{\linewidth}
    Title
    \end{minipage}
}

\begin{multicols}{2}
  \lipsum[1-5]
\end{multicols}

\end{document}

Note that this eliminates any horizontal padding, so text inside will now look like this:

Result

But this is easily fixed by adding \hspace{1mm} before Title:

Hspace

share|improve this answer
    
The title will be centered eventually. And no I didn't need the minipage except to specify the width. –  T. Verron Oct 26 '12 at 19:14
    
@T.Verron: You can center it by using \framebox[\linewidth][c] or just \framebox[\linewidth]. –  Silex Oct 26 '12 at 19:17
    
I meant "horizontally centered", as a justification about why I don't really care about horizontal padding. Thanks for your answer! –  T. Verron Oct 26 '12 at 19:22
add comment

It's always good to create macros for things you might be doing often, for the sake of consistency (see Consistent typography). So, perhaps \myheading or \myheading* could be used:

enter image description here

\documentclass{article}
\usepackage{multicol,xparse,lipsum}% http://ctan.org/pkg/{multicol,xparse,lipsum}
\NewDocumentCommand{\myheading}{s O{\bfseries} m}{%
  \noindent
  \IfBooleanTF{#1}
    {\framebox[\linewidth][l]{#2#3}}
    {\makebox[\linewidth]{%
      \framebox[\dimexpr\linewidth+2\fboxsep+2\fboxrule][l]{#2#3}}}%
}
\begin{document}

\myheading{Title}
\begin{multicols}{2}
  \lipsum[6]
\end{multicols}

\myheading[\slshape]{\hfill Title\hfill}
\begin{multicols}{2}
  \lipsum[6]
\end{multicols}

\myheading*{Title}
\begin{multicols}{2}
  \lipsum[6]
\end{multicols}

\myheading*[\itshape]{\hfill Title}
\begin{multicols}{2}
  \lipsum[6]
\end{multicols}

\end{document}

The marginal difference between \myheading and \myheading* is that the former sets the title to align with the text block, while the latter sets the framed box to align with the text block (as in Silex' answer). The optional argument sets the font of the heading - bold or \bfseries by default. xparse provides the macro programming interface.


Some more detail: The s O{<stuff>} m specification for \myheading allows for

  • a starred variant of \myheading because of the s parameter. There, it essentially creates two macros \myheading and \myheading*;
  • an optional first argument - due to the O{<stuff>} parameter - that defaults to <stuff> if it's not supplied; and
  • a mandatory argument - as a result of m - used to typeset the title.

\IfBooleanTF is used to condition between the supplied argument. In this case, I've used \IfBooleanTF{#1}, so the function will condition on whether you supplied a * or not. If you did, the first condition is executed:

\framebox[\linewidth][l]{#2#3}

This will set #2 - the optional argument (defaulting to \bfseries if it is not supplied) - and #3 - the mandatory argument (title). If no star is supplied, the second condition is executed:

\makebox[\linewidth]{%
  \framebox[\dimexpr\linewidth+2\fboxsep+2\fboxrule][l]{#2#3}}

This sets a box of width \linewidth and inside it a \framebox. The reason for the \makebox is to make TeX think that the box will only be as wide as the line, even though it actually stretches beyond that. The default behaviour of \makebox[\linewidth] is to centre its contents, similar to \makebox[\linewidth][c]. The \framebox has a width of \linewidth+2\fboxsep+2\fboxrule since the interior content has to fit within \linewidth, but there's an added frame separation and rule on both sides of it - left and right. Just like with \makebox, \framebox has an additional optional argument for horizontal adjustment, set to [l]eft aligned.

share|improve this answer
    
Thanks for your answer! Those are interesting alternatives, and the ultimate goal is indeed to have a macro for my headings, mainly because I will have many other things to tweak in these headers. However, it seems to me as if your macro \myheading* does exactly the same as the answer Silex offered, with \framebox (except for the ability to add a font specification). I doubt all these lines of code I don't understand are useless, so if there's a way you can explain them simply, I'd be really curious. –  T. Verron Oct 26 '12 at 19:21
    
@T.Verron: I've added some explanation of my code. –  Werner Oct 26 '12 at 19:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.