2

I am planning to write a lot of letters in the near future. Since I am going to prepare the letters with LaTeX I also want to print the envelope addresses with it. All the address information is in a vCard file. Is there an easy way to read the addresses and print them on an envelope (By easy I mean: Has anyone already done it before)? Basically, vCard data is similar to csv data but differs in its regularity. If not, what would be the best solution?

The only question regarding vCards is this one: Problem creating a proper qrcode with vcard info. It looks like I cannot use my raw address data without reformatting first.

Another question uses addresses in a custom format: How to print addresses. Should I just try to export the addresses with a different tool?

Here is an example of how my vCard data looks like (I deleted some unrelated lines):

BEGIN:VCARD
VERSION:3.0
PRODID:-//ownCloud//NONSGML Contacts 0.3//EN
N:Doe;John;;;
FN:John Doe
ADR;TYPE=HOME:;;Ivory Road 31;Capetown;;123456;
END:VCARD
2
  • 2
    I would say that TeX/LaTeX isn't really good in reading such structured data unless you write a parser of your own
    – user31729
    Oct 6, 2015 at 14:18
  • 1
    I agree with @ChristianHupfer and would recommend this architecture: use a language that has support for reading vCards and emit each address like \vcard{Doe}{John}{Ivory Road 31}{Capetown}{123456}. Then write a LaTeX document that implements a \vcard command that typesets the address. This keeps the two problems separate: reading the vCard format and typesetting addresses. Oct 6, 2015 at 14:30

1 Answer 1

1

enter image description here

If the vcard is in jd.txt then

\documentclass{article}
\makeatletter
\newread\vcardin
\errorcontextlines1111
\def\vcardinput#1{%
\begin{tabular}{@{}l@{}}%
\toks@{}%
\immediate\openin\vcardin=#1\relax
\loop
\ifeof\vcardin
\else
\immediate\read\vcardin to \tmp
\ifx\endsloppypar\tmp\else
\expandafter\vcsplit\tmp\relax
\fi
\repeat
\the\toks@
\end{tabular}}

\def\vcsplit#1:#2\relax{%
 \gdef\tmpb{#1}%
  \ifx\tmpb\vcFN\toks@\expandafter{\the\toks@#2\\}\fi
  \ifx\tmpb\vcAD
    \edef\tmpb{\breaksemi#2;\relax}%
    \toks@\expandafter{\the\expandafter\toks@\tmpb\\}\fi
}
\def\vcFN{FN}
\def\vcAD{ADR;TYPE=HOME}

\def\breaksemi#1;#2\relax{%
\ifx\vsplit#2\vsplit\else
#1\noexpand\\\breaksemi#2\relax\fi}

\begin{document}

\vcardinput{jd.txt}

\end{document}
3
  • Wow, I did not expect to get a working answer after the comments above. Is it also possible to create multiple output files with multiple address files in the folder?
    – mrub
    Oct 8, 2015 at 14:14
  • @mrub no tex can't do multiple output. If you know each letter is 1-page you can do one per page then use an external pdf chop tool to split it up. (multiple input is OK) Oct 8, 2015 at 15:59
  • Since I want to print the addresses on envelopes I could just make one file with as many pages as I have recipients? This should be even better because I only have to print one file…
    – mrub
    Oct 8, 2015 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.