3

First, my example:

\documentclass[]{article} 

\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{enumitem}

\newcommand\codeitem[1]{
    \item[{
        \framebox[\textwidth][l]{
        \tt
        {#1}
        }
     }]
    }

\begin{document}
    \blindtext
    \begin{description}
        \codeitem{+ here() : void}
            Followed by some paragraphs of text. Do you see the text is too near to the box?
        \codeitem{\# there() : void}
            I somehow have to add vertical space here.
    \end{description}
    \blindtext
\end{document}

This renders as follows: too little space after the box

I think the idea is clear. Alas, there's too little vertical space after the description label. How can I set the vertical space between the item label and its text? I've tried setting \vspace{11mm} but this has the same effect as using topsep=11mm or partopsep=11mm on the description. It only increases the distance between list items or the list and the paragraph before.

4

Add some vertical space:

\documentclass{article}

\usepackage[english]{babel}
\usepackage{blindtext}
\usepackage{enumitem}

\newcommand\codeitem[1]{%
    \item[{%
        \framebox[\textwidth][l]{%
        \normalfont\ttfamily
        #1%
        }\vspace{3pt}%
     }]
    }

\begin{document}
    \blindtext
    \begin{description}[style=nextline]
        \codeitem{+ here() : void}
            Followed by some paragraphs of text. Do you see the text is too near to the box?
        \codeitem{\# there() : void}
            I somehow have to add vertical space here.
    \end{description}
    \blindtext
\end{document}

Note the style=nextline option to avoid overfull boxes and also the % to protect end-of-lines and avoid they produce space in output. The command \tt is obsolete and deprecated: use \ttfamily. The \normalfont command is to avoid requesting for boldface typewriter type.

enter image description here

A refinement might be as follows:

\newcommand\codeitem[1]{%
    \item[{%
        \framebox[\textwidth][l]{%
        \ttfamily\ttstrut
        #1%
        }\vspace{3pt}%
     }]
    }

\newcommand{\ttstrut}{%
  \vrule width0pt
         height1.5ex
         depth.5ex
}

With this, we'd get (magnified for better showing the effect, adding also a letter with a descender)

enter image description here

The boxes will all have the same total height; the top and bottom of an x will have the same distance from top and bottom rule respectively. Ascenders and descenders will occupy the otherwise white space.

  • 1
    looks good, but if anything in the code happens to have a real descender ("f", "p", "y", ...), the height of the boxes will be noticeably different. you may want to consider taking that into account. – barbara beeton Jun 2 '14 at 20:13
  • @barbarabeeton That's a good point. I have not yet happened to get into this special situation. Do you have suggestions concerning different-heighted boxes? Thank you for your overall notices. It's great to see ´´style=nextline´´ and ´´\vspace´´ do the trick. – marc Jun 2 '14 at 20:18
  • @user2149498 -- if there isn't any instance of a boxed item with a descender, i think i'd stick with this -- it looks nice. but maybe an alternate form, with \vphantom{y} or similar, could be defined for use when a bunch of items that are visually close together includes one or more that contain a descender. for a multi-page document, maybe this would be a last-pass decision. – barbara beeton Jun 2 '14 at 20:26
  • @barbarabeeton I'll add a \ttstrut. – egreg Jun 2 '14 at 20:35

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.