# Creating codebook with justified entries using \hfill or other method

I am trying to create a codebook for my thesis, but am struggling to format it properly. I am trying to have the names of the variables all left justified and have the variable definitions also left justified with each other. I have added space with \hfill currently but this is not justifying the definitions and the length of some of my longer definitions causes the problem shown below. I would like all definition text to stay on the right side instead of snaking underneath the variable name. Any suggestions would be greatly appreciated!

• You can use the package longtable or a more efficient way the package glossaries – Marco Daniel Mar 19 '17 at 20:17
• Welcome to TeX.SE! you can form this as list, for example description – Zarko Mar 19 '17 at 20:25
• Thank you. I think the description function will work well. – Elle P Mar 19 '17 at 22:17

What was missing in what you tried was a \parbox, so that some like {\bfseries mycodebook}\hfill\parbox[t]{8cm}{my long text} could be enough.

To simplify this a bit, the example below show a \code macro with two argument (code and definition) that work only in a codebook environment to avoid indentation and set some vertical spacing between definitions. The environment allow an optional argument that should be the longest code, in order to adjust automatically the width of the definitions.

Note that like in lists, once the environment ended, the first next paragraph could be not indented. This could have sense since the begin of the paragraph will be clear. However, if you want change this behavior simply left a blank line after the environment.

\documentclass{article}
\usepackage{lipsum} % dummy text

\newlength\cbl
\newenvironment{codebook}[1][rob\_avprison1]{
\settowidth{\cbl}{#1}
%\setlength{\cbl}{#1}
\parskip1em plus .3em minus .2em
\parindent0pt
\def\code##1##2{{\bfseries ##1}\hfill%
\parbox[t]{\dimexpr\linewidth-2em-\cbl}{##2}\par}}{\noindent}

\begin{document}

\lipsum[2]

\begin{codebook}
\code{rob\_mannim0}{\lipsum[66]}
\code{rob\_avprison1}{\lipsum[75]}
\end{codebook}
\lipsum[8] % next paragraph not indented

\begin{codebook}[book0]
\code{book0}{\lipsum[34]}
\code{book0}{\lipsum[34]}
\end{codebook}

\lipsum[6-10] % paragraph indented

\end{document}

The main difference with a list environment is that page breaks are not allowed inside a \code item. If you want this, one easy alternative, also with optionally adjustable space for the code, could be use the enuitem package:

\documentclass{article}
\usepackage{enumitem,calc}
\usepackage{lipsum}
\newenvironment{codebook}[1][xxxxxxxxxxx]{%
\begin{description}[leftmargin=\widthof{#1}+2em, style=nextline]}
{\end{description}}
\begin{document}
\lipsum[2]
\begin{codebook}
\item[rob\_mannim0]    \lipsum[66]
\item [rob\_avprison1] \lipsum[75]
\end{codebook}
\lipsum[4]
\begin{codebook}[rob\_avprison1\hskip2em]
\item[rob\_mannim0]    \lipsum[66]
\item [rob\_avprison1] \lipsum[75]
\end{codebook}
\lipsum[6-10]
\end{document}
• Ah! Thank you! I found \hbox, but \parbox actually accomplishes what I wanted. – Elle P Mar 20 '17 at 15:05

This solution requires you to guess the width needed for the definitions.

\documentclass{article}
\usepackage{array}
\usepackage{longtable}
\usepackage{blindtext,showframe}% MWE only

\newcolumntype{d}{>{\textbf\bgroup}l<{\egroup}}
\setlength{\LTleft}{0pt}
\setlength{\LTright}{0pt}

\begin{document}
\begin{longtable}{@{}d@{\extracolsep{\fill}}p{3.5in}@{}}
name & \blindtext \\[\baselineskip]
another name & \blindtext
\end{longtable}

\end{document}

This solution is more like a list. It also requires you to guess or calculate the length of the longest name.

Note, \itemsep and \labelsep are traditional, not compulsory.

\documentclass{article}
\usepackage{blindtext,showframe}% MWE only

\newlength{\maxwidth}
\settowidth{\maxwidth}{\textbf{longest name}}

\newcommand{\defineit}[1]{\ifvmode\else\vskip\itemsep\fi
\par\leavevmode
\hspace{-\leftskip}\makebox[\leftskip][l]{\textbf{#1}}\ignorespaces}

\newenvironment{definitions}%
{\setlength{\parindent}{0pt}%
\setlength{\leftskip}{\dimexpr \maxwidth+\labelsep}%
\let\item=\defineit
\ignorespaces
}{\par\ignorespaces}

\begin{document}
\begin{definitions}
\item{name}\blindtext
\item{longest name}\blindtext
\end{definitions}

\end{document}
• Thank you very much. This is exactly the format I was after. – Elle P Mar 19 '17 at 22:17