2

I have the following macro:

\newcommand{\mycommand}[4]{
\begin{tabbing}
    \hspace{2cm} \= \kill
    \textbf{#1} \>  {#3} \\
    \textbf{#2} \>      
            \begin{minipage}{\smallertextwidth}
                \vspace{1mm}
                \textbf{label:} #4
            \end{minipage}
        \end{tabbing}

}

what happens is that the string given by \textbf{#2} is vertically aligned to the middle of the minipage on its side, while I'd like to have it aligned to the top of the minipage. How can achieve such an alignment?

Werner
  • 603,163
dario
  • 303
  • Why do you need a minipage? Will the text in #4 span multiple lines (like a paragraph)? – Werner Nov 28 '15 at 02:02
  • I just modified an environment I found in a document template. The minipage is needed because in the tabbing environment lines are not broken: a long sentence would be on a single line causing overfull hbox – dario Nov 28 '15 at 13:10

2 Answers2

2

My suggestion would be a to use a tabular-like construction. And, to avoid using arbitrary lengths, you can leave the length-calculations up to LaTeX when using tabularx:

enter image description here

\documentclass{article}

\usepackage[nopar]{lipsum}
\usepackage{tabularx}

\newcommand{\mycommand}[4]{%
  \begin{tabularx}{\textwidth}{@{}>{\bfseries\arraybackslash}p{2cm} X @{}}
    #1 & #3 \\
    #2 & \textbf{label:} #4
  \end{tabularx}}

\begin{document}

\noindent
\mycommand{One}{Two}{Three}{Four}

\noindent
\mycommand{One}{Two}{\lipsum[3]}{\lipsum[4]}

\end{document}

The p- and X-columns have a default top-alignment with the surrounding row entries.

The choice of using tabularx (or something tabular) above a minipage stems from the typical problem of baseline alignment. It's just easier/more convenient when using a table. See How to keep a constant baseline skip when using minipages (or \parboxes)?](How to keep a constant baselineskip when using minipages (or \parboxes)?)

Note that the above construction will not break across the page boundary. That was also the case with your minipage construction, so I doubt it would be a problem in your use case

Werner
  • 603,163
2

While tabbing can reveal useful at times, it is not for this application, where a simpler tabular environment suffices:

\documentclass{article}

\usepackage{lipsum} % just for the example

\newlength{\mycommandwidth}
\setlength{\mycommandwidth}{2cm}% or what you prefer as default

\newcommand{\mycommand}[5][\mycommandwidth]{% <--- don't forget it
  \par\noindent
  \begin{tabular}{@{} l p{\dimexpr\columnwidth-2\tabcolsep-#1} @{}}
    \makebox[#1][l]{\bfseries #2} & #4 \\
    \makebox[#1][l]{\bfseries #3} & \textbf{label:} #5
  \end{tabular}%
  \par
}

\begin{document}

\mycommand{One}{Two}{Three}{Four}

\mycommand[1cm]{One}{Two}{Three}{Four}

\mycommand{One}{Two}{\lipsum[2]}{\lipsum[2]}

\mycommand[1cm]{One}{Two}{\lipsum[2]}{\lipsum[2]}

\end{document}

enter image description here

egreg
  • 1,121,712