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.

What I wanted to achieve was this: enter image description here
I defined a new command like this:

\newcommand{\details}[2]{
#1 \hspace{2cm} : #2 \\
}

The full MWE is as follows:

\documentclass[12pt]{article}
\newcommand{\details}[2]{
#1 \hspace{2cm} : #2 \\
}
\begin{document}
\noindent
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY}
\end{document}

What I get is this:

enter image description here
Now, two things:
1) Even after adding \noindent, there is a slight indent
2) The colons aren't aligned.

What I wanted the command to do was, what ever be text(Name, DOB, etc.), leave 2cm after it and then print the colon. Also, if the text is two long, then the text must break and appear after the colon only

How do I do it?

share|improve this question
    
You can also try the listliketab package. It provides pretty much everthing you need for these kind of problems and it's easy to handle. –  Lorzen Aug 6 at 11:32

4 Answers 4

up vote 6 down vote accepted

You can use a tabular as Jubobs has mentioned. This is an attempt to correct your approach. You are adding a horizontal space of 2cm after the first word. Then the : is placed 2cm to the right of the first word. That is why they are not aligned vertically. Remedy is to put the first word inside a box of some length and then, put the :. You can actually calculate the width of widest first word and set the width of your box.

\documentclass[12pt]{article}
\usepackage{calc}
\newlength{\mylen}
\setlength{\mylen}{\widthof{Address for Communication}}
\newcommand{\details}[2]{%
\makebox[\mylen][l]{#1} \,: #2 \\
}
\begin{document}
\noindent
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY}
\end{document}

enter image description here

Here is a solution using minipages:

\documentclass[12pt,draft]{article}
\usepackage{calc,showframe}
\newlength{\mylen}
\setlength{\mylen}{\widthof{Address for Communication}}
\newlength{\mylength}
\setlength{\mylength}{\widthof{:}}
\newcommand{\details}[2]{%
\begin{minipage}[t]{\mylen}
#1
\end{minipage}%
\hspace{2mm}
\begin{minipage}[t]{\dimexpr\mylength+2mm}
:
\end{minipage}%
\begin{minipage}[t]{\dimexpr\textwidth-6mm-\mylen-\mylength\relax}
 #2 \par
\end{minipage}
\par\noindent
\ignorespaces
}
\begin{document}
\noindent
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{This is going to be some long address This is going to be some long address}
\end{document}

This is a tabular solution (improvised Jubobss' solution)

\documentclass[12pt]{article}
\usepackage{calc,array}
\newlength{\mylen}
\setlength{\mylen}{\widthof{Address for Communication}}
\newcommand{\details}[2]{%
\begin{tabular}[t]{@{}p{\mylen}!{:}p{\dimexpr\textwidth-\mylen-4\tabcolsep\relax}}
#1 & #2 
\end{tabular}%
\par\noindent
\ignorespaces
}
\begin{document}
\noindent
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{This is going to be some long address This is going to be some long address}
\end{document}

And this one uses tabularx:

\documentclass[12pt]{article}
\usepackage{array,tabularx}
\newcommand{\details}[2]{%
#1 & #2  \\
}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}[t]{@{}>{\bfseries}l!{:}X}
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{This is going to be some long address This is going to be some long address}
\end{tabularx}%
\end{document}

enter image description here

share|improve this answer
    
can you come for a personal chat please –  subham soni Aug 6 at 8:34

The "slight indent" is caused by the spurious space at the beginning of \details's definition.

If you want a tabular-like output, why not use a tabular environment?

enter image description here

\documentclass[12pt]{article}

\newcommand*{\details}[2]{% <--- there was a spurious space here
  \textbf{#1} & : #2 \\
}

\begin{document}
\begin{tabular}{l@{\hspace{2cm}}l}
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY}
\end{tabular}
\end{document}
share|improve this answer
    
@subhamsoni What do you mean? –  Jubobs Aug 6 at 8:22
    
A Table will have cells (rows and columns) but, here it has a similar structure too but its not a table. Confusing right? –  subham soni Aug 6 at 8:23
    
@subhamsoni I'm not sure I understand, but perhaps you're confusing table and tabular environments. The important question is: does the output suit you? –  Jubobs Aug 6 at 8:25
    
Yes. I am satisfied with it –  subham soni Aug 6 at 8:26

You can also use \parbox. The code will look like:

\documentclass[12pt]{article}
\newcommand{\details}[2]{\noindent\strut%
  \parbox[t]{.48\textwidth}{\raggedright\strut#1\strut}\ :\ % add 2cm if you wish
  \parbox[t]{.5\textwidth}{\raggedright\strut#2\strut}\par}
\begin{document}
\pagestyle{empty}
\details{Name}{Subham Soni}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY}
\end{document}

Output

enter image description here

share|improve this answer

If you prefer parboxes instead of tabulars. In any case I think tabular is better because it computes columns widths automatically, while in this case you need to know the largest line.

\documentclass[12pt]{article}

\newcommand{\details}[2]{%
\parbox[t]{3cm}{\raggedright \strut#1\strut} : \parbox[t]{3cm}{\raggedright \strut#2\strut}\\
}

\begin{document}
\noindent
\details{Name}{Subham Soni and some more text to see what happens}
\details{Date of Birth}{March 22, 1994}
\details{Nationality}{Indian}
\details{Address for Communication}{XXXYYY and some more text}
\end{document}

enter image description here

share|improve this answer
    
You should, at least, add \strut before and after #2; but the vertical spacing between stacked parboxes will not be uniform. –  egreg Aug 6 at 8:34
    
@egreg thank you for the advice. –  Ignasi Aug 6 at 9:26

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.