# Can we define a \newcommand that outputs text within a fixed width box?

I am creating a document which has a fairly standard structure. So I have created two commands that help me format it:

\documentclass{article}
\usepackage{xltxtra}
\newcommand{\witem}[2]{{#1 #2}}
\newcommand{\ws}[2]{{#1\textsuperscript{#2}}}
\begin{document}

\setlength{\parindent}{0cm}
\witem{Jan 1}{OHP} \ws{20}{5,5} \ws{26}{5} \ws{32}{5,5,5} \\
\witem{Apr 9}{BP} \ws{20}{5,5} \ws{33}{5} \ws{46}{5,3,3}

\end{document}


Is it possible to change the \witem{Jan 1}{OHP} that is outputting "Jan 1" "OHP", always outputs the two blocks as 2cm + 3cm? (I don't need to worry about line wrapping or anything like that.

You can use \makebox:

\newcommand{\witem}[2]{%
\makebox[2cm][l]{#1}%
\makebox[3cm][l]{#2}%
}


Full example:

\documentclass{article}

\newcommand{\witem}[2]{%
\makebox[2cm][l]{#1}%
\makebox[3cm][l]{#2}%
}
\newcommand{\ws}[2]{{#1\textsuperscript{#2}}}

\begin{document}

\witem{Jan 1}{OHP} \ws{20}{5,5} \ws{26}{5} \ws{32}{5,5,5}

\witem{Apr 9}{BP} \ws{20}{5,5} \ws{33}{5} \ws{46}{5,3,3}

\end{document}


Change [l] into [r] if you want right alignment in the allotted area or omit it if you want centering.

• So many good answers. I am ticking this one purely because it is the simplest. Thanks. – Jacob Apr 9 '17 at 10:32
• @egreg deserved as much as usual:-) – David Carlisle Apr 9 '17 at 18:08

For example use a \parbox with has the relevant width.

The example shows the usage with two defined lengths for this, which can be changed easily for any other purpose of the \witem command.

Other possibility: Use a tabularx with p and X - columntype approaches:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tabularx}
\usepackage{xltxtra}

\newlength\firstboxwidth
\newlength\secondboxwidth

\setlength{\firstboxwidth}{2cm}
\setlength{\secondboxwidth}{3cm}

\newcommand{\witem}[2]{\parbox{\firstboxwidth}{#1}\parbox{\secondboxwidth}{#2}}

\newcommand{\wtabitem}[2]{{#1} & {#2} &}
\newcommand{\ws}[2]{{#1\textsuperscript{#2}}}
\begin{document}

\setlength{\parindent}{0em}

\witem{\rule{\firstboxwidth}{0.5cm}}{\color{red}\rule{\secondboxwidth}{0.5cm}}

\witem{Jan 1}{OHP} \ws{20}{5,5} \ws{26}{5} \ws{32}{5,5,5}

\witem{Apr 9}{BP} \ws{20}{5,5} \ws{33}{5} \ws{46}{5,3,3}

And now the tabularx approach:

\begin{tabularx}{\linewidth}{@{}p{\firstboxwidth}@{}p{\secondboxwidth}@{}XXX}

\wtabitem{Jan 1}{OHP} \ws{20}{5,5} & \ws{26}{5}& \ws{32}{5,5,5}  \tabularnewline

\wtabitem{Apr 9}{BP} \ws{20}{5,5} &\ws{33}{5}& \ws{46}{5,3,3} \tabularnewline

\end{tabularx}

\end{document}


• I prefer the tabularx approach – user31729 Apr 9 '17 at 10:16
• For what looks like it might be a basic table of numbers, tabularx would certainly be logical. However, what I am ultimately trying to achieve is going to need more flexibility than can be done in a table. Thanks so much! – Jacob Apr 9 '17 at 10:31
• @Jacob: You noticed that I have actually posted two solutions (in one file, however)? – user31729 Apr 9 '17 at 10:33
• For what its worth, I also like your answer, and upvoted you, and appreciate your time to make this response. – Jacob Apr 9 '17 at 23:36

possibly the most direct implementation of your request would be via \makebox1

\documentclass{article}
%\usepackage{xltxtra}
\newcommand{\witem}[2]{{\makebox[2cm][l]{#1}\makebox[3cm][l]{#2}}}
\newcommand{\ws}[2]{{#1\textsuperscript{#2}}}
\begin{document}

\setlength{\parindent}{0cm}
\witem{Jan 1}{OHP} \ws{20}{5,5} \ws{26}{5} \ws{32}{5,5,5} \\
\witem{Apr 9}{BP} \ws{20}{5,5} \ws{33}{5} \ws{46}{5,3,3}

\end{document}