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.

Often when writing letters and articles, I need to print my address at the beginning or end of the document. I do this in the following ugly way:

\documentclass{article}
\begin{document}
\noindent My name\\
\noindent My university\\
\noindent My department\\
\noindent My address\\
\noindent My e-mail\\
\end{document}

Clearly there must be a better way to do this than having to repeat \noindent ... \\ for every line. I'm thinking I could define a new command \myaddress that will simply print all of those lines:

\documentclass{article}
% define address command here
\begin{document}
\myaddress
\end{document}

Or at the very least define a new environment that takes care of the indentation and line break:

\documentclass{article}
% define address environment here
\begin{document}
    \begin{address}
        My name
        My university
        My department
        My address
        My e-mail
    \end{address}
\end{document}

But I have no idea how to do either.

share|improve this question
1  
What does make you think that \noindent is necessary after the first line? It isn't. Of course, the last \\ is not necessary either (better, it's wrong). –  egreg Feb 18 at 20:58
    
Unless I've overlooked something, you can also use \obeylines in the start of your macro definition rather than putting a \\ after each line. –  ChrisS Feb 19 at 0:38
    
@ChrisS: I can't get that to work. Can you? –  Sverre Feb 19 at 9:14
1  
@Sverre: It turns out it was overlooking something: \obeylines doesn't work in a macro definition. –  ChrisS Feb 19 at 9:19
add comment

4 Answers 4

You can easily do something as simple as

\newcommand\printmyaddress{%
\begingroup
\parindent 0pt
\myname       \par
\myuniversity \par
\mydepartment \par
\myaddress    \par
\myemail      \par
\endgroup}

and then set those macros accordingly. Put it in a .sty file and you'd be good enough to go (assuming the formatting is done as you like it).

Maybe it's more fun to be more elaborate, however. One idea would be a key-value approach that allows you to have default values that can be overriden piece by piece if you like. There are many options this way; here's one that uses l3keys:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{xparse}

% from here to \ExplSyntaxOff, you could put this in a separate .sty
% to keep things tidy...
\ExplSyntaxOn
% get things set up with defaults:
\keys_define:nn { svdetails }
 {
  name       .tl_gset:N = \g_svdetails_name_tl,
  university .tl_gset:N = \g_svdetails_university_tl,
  department .tl_gset:N = \g_svdetails_department_tl,
  address    .tl_gset:N = \g_svdetails_address_tl,
  email      .tl_gset:N = \g_svdetails_email_tl,
% set the default values (change to real values) 
  name       .initial:n = NAMEDEFAULT,
  university .initial:n = UNIDEFAULT,
  department .initial:n = DEPTDEFAULT,
  address    .initial:n = ADDRESSDEFAULT,
  email      .initial:n = EMAILDEFAULT
 }

\NewDocumentCommand{\biodetails}{ m }
 {
  \keys_set:nn { svdetails } { #1 }
 }

% How do we want things to look when printed? 
% (Nothing fancy here: just 'zero' the \parindent and print
% everything in a new paragraph. But it would be easy to change.)
\NewDocumentCommand{\printdetails}{ }
 {\begingroup
   \parindent 0pt
   \g_svdetails_name_tl       \par
   \g_svdetails_university_tl \par
   \g_svdetails_department_tl \par
   \g_svdetails_address_tl    \par
   \g_svdetails_email_tl      \par
   \endgroup}

\ExplSyntaxOff

% override document defaults; not strictly necessary since we
% already set defaults above...
\biodetails{
  name=Sverre Sverre,
  university=University of Somewhere,
  department=Department of XYZ,
%  address=Unknown Address,
  email=sverre@sverre.com,
}

\begin{document}

\printdetails

\end{document}
share|improve this answer
add comment

Perhaps

\documentclass{article}

\newcommand*{\myaddress}{%
\begin{flushleft}
\setlength{\tabcolsep}{0pt}%  For flushing at least a little bit
\begin{tabular}{l}
My Name \\
My university \\
My department \\
My address \\
My e-mail \\
\end{tabular}
\end{flushleft}
}% End of \newcommand*

\begin{document}
  \myaddress
\end{document}

is a possible 'quick' solution. The linespacings can be set by redefinition of \arraystretch for example, if needed.

share|improve this answer
    
Using a tabular environment will, however, not flush the text to the margin. But I used your suggestion as a template, and (as a quick fix) put my entire address, with \noindent ...\\, inside \newcommand{\myaddress}{}. –  Sverre Feb 17 at 18:29
    
@Sverre: Ok, I see, that I missed flushing to the margin... perhaps, one could do with setting \tabcolsep to zero. –  Christian Hupfer Feb 17 at 18:30
1  
@Christian Instead of changing a global variable to a bad value, disable the seps locally by changing {l} to {@{}l@{}} –  tohecz Feb 17 at 18:39
    
@tohecz: That's a nice idea, but if I use another table after my \myaddress command, the tabcolsep value is restored to original value -- it is changed within a \newcommand. –  Christian Hupfer Feb 17 at 18:42
1  
@Christian Your setting to \tabcolsep is indeed local because it's issued inside a flushleft environment (not because it's changed within \newcommand). But the @{}l@{} method is much easier. –  egreg Feb 18 at 21:09
show 1 more comment

A left-aligned \Longstack will work here. Depending on how you need the vertical placement of the item, a \Longunderstack may be preferable to \Longstack, though in many situations, it won't matter which is chosen.

\documentclass{article}
\usepackage[usestackEOL]{stackengine}
\begin{document}
\noindent\Longstack[l]{
 My name\\
 My university\\
 My department\\
 My address\\
 My e-mail}
\textbf{versus}
\Longunderstack[l]{
 My name\\
 My university\\
 My department\\
 My address\\
 My e-mail}
\end{document}

enter image description here

share|improve this answer
add comment

Based on the suggestions here and on Ulrike's comment to Cancel the use of \baselineskip when at the top of page, the following seems to be a good solution.

\documentclass{article}
\newcommand{\myaddress}{\par\vspace{\baselineskip}\vbox{\parindent = 0em%
    My name\\%
    My university\\%
    My department\\%
    My address\\%
    My e-mail}}
\begin{document}
\myaddress
\end{document}
share|improve this answer
add comment

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.