1

I am not sure this is possible nor how I can accomplish this and thus I can't include a MWE. I assume though that stuff like this is used to create LaTeX packages.

The idea is to add a table row (or any text for that matter) only if the user has defined a variable. Something like this:

% as part of a different file, say some sort of class I'm defining \newcommand\address[1]{\def\theaddress{#1}} \address{\theaddress{}} % I guess this defines an empty definition? % later on the same document, inside a table (this is pseudo-code) if \theaddress is not empty, write { Address & \theaddress \\ } else don't add anything % later on the actual document I can define the address to whatever I want. \address{This is my address} 

UPDATED

So I came up with this working example. Say you have the user document as:

\documentclass[]{project} \contact{\footnotesize{John Doe}} \contactb{\footnotesize{John Appleseed}} \address{\footnotesize{PME}} \addressb{\footnotesize{BME}} \phone{\footnotesize{...}} \phoneb{\footnotesize{...}} \email{email1} \emailb{email2} % Reference top left \begin{document} \makeheader \end{document} 

And a project.cls as follows:

\NeedsTeXFormat{LaTeX2e} \ProvidesClass{tuletter} \LoadClass[a4paper,10pt]{article} \RequirePackage[absolute]{textpos} % positioning the header elements \RequirePackage[usenames,dvipsnames]{color} % use gray color for default values and labels % Font color for default values (values not defined in LaTeX file) \newcommand{\tudefault}[1]{\textcolor{Gray}{#1}} \newcommand\contact[1]{\def\tucontact{#1}} \contact{\tudefault{contact name}} \newcommand\contactb[1]{\def\tucontactb{#1}} \contactb{\tudefault{contact name 2}} \newcommand\address[1]{\def\tuaddress{#1}} \address{\tuaddress{your address}} \newcommand\addressb[1]{\def\tuaddressb{#1}} \addressb{\tuaddressb{second line of your address}} \newcommand\phone[1]{\def\tuphone{#1}} \phone{\tudefault{+31 (0)15 278 \ldots}} \newcommand\phoneb[1]{\def\tuphoneb{#1}} \phoneb{\tudefault{+31 (0)15 278 \ldots}} \newcommand\email[1]{\def\tuemail{#1}} \email{\tudefault{\ldots @tudelft.nl}} \newcommand\emailb[1]{\def\tuemailb{#1}} \emailb{\tudefault{\ldots @tudelft.nl}} %%% Labels \newcommand{\turefval}[1]{\small{#1}} \newcommand{\tureflab}[1]{\textcolor{Gray}{\scriptsize{#1}}} \newcommand\tudatename{Date} \newcommand\tucontactname{Contact} \newcommand\tuaddressname{Your address} \newcommand\tuphonename{Phone} \newcommand\tuemailname{E-mail} \newcommand{\makeheader}{% \textblockorigin{0cm}{0cm} % Reference \begin{textblock*}{100mm}(25mm, 9mm) \begin{tabular}{r@{ }cc} \tureflab{\tucontactname} & \turefval{\tucontact} & \turefval{\tucontactb} \\ \tureflab{\tuaddressname} & \turefval{\tuaddress} & \turefval{\tuaddressb} \\ % & \turefval{\tuaddresss} & \turefval{\tuaddressbs} \\ \tureflab{\tuphonename} & \turefval{\tuphone} & \turefval{\tuphoneb}\\ \tureflab{\tuemailname} & \turefval{\tuemail} & \turefval{\tuemailb} \\ % \tureflab{\tusubjectname} & \turefval{\tusubject} \\ \end{tabular} \end{textblock*} } \endinput 

which I stripped from a working template. The result of this code is:

enter image description here

But now everything is hardcoded so that 2 people show up at the top. My idea is to extend this code so that it works with only 1 or even more people, while keeping the same format AND allow the inclusion of more information per field (for example, if the address needs to take more space so that it fits within 2 or more lines).

6
  • It is not the same to have a macro not defined, than having it defined but with "empty" value. Related question: tex.stackexchange.com/questions/44919/… Commented Oct 22, 2015 at 9:39
  • You could (should) have provided an MWE that is a skeleton document that worked (or gave an error) as far as you could get, makes it much easier for people to test answers Commented Oct 22, 2015 at 10:13
  • @JLDiaz it's not the same, as the document should compile even it the macro hasn't been defined. Commented Oct 22, 2015 at 10:29
  • @DavidCarlisle how to come up with a MWE of something you have absolutely no idea about? Commented Oct 22, 2015 at 10:30
  • you could put \documentclass at the top and \begin{document} \end{document} around a sketch of the code that set \address and used \theaddress (presumably) in the middle of a tabular environment. Then reported the error and ask that people ammend it so the error goes away. Commented Oct 22, 2015 at 11:35

1 Answer 1

3

\address{\theaddress{}} % I guess this defines an empty definition?

No, that defines an infinite loop, as it is

\def\theaddress{\theaddress{}} 

so \theaddress has definition \theaddress{} so if you try to expand \theaddress the first thing it will do is try to expand \theaddress again, pushing {} ahead on the input stack, and will do this forever until you run out of stack.

Use an initial setting of

\def\theaddress{} 

then you can use

\ifx\theaddress\@empty\else &Address& \theaddress\\ \fi 

Or often better: do not have any test at all and just use

\theaddress 

at that point with an initial definition of

\def\theaddress{} 

so it does nothing, but define

\newcommand\address[1]{\def\theaddress{Address&#1\\}} 

so that if \address has been used then \theaddress will make the complete row.


enter image description here

Avoiding putting font sizes and things into the document:

\documentclass[]{project} \contact{John Doe} \contact{John Appleseed} \contact{Me} \address{PME} \address{BME} \address{here} \phone{...} \phone{..} \phone{123} \email{email1} \email{email2} \email{me@there} % Reference top left \begin{document} \makeheader \end{document} 

with this class

\LoadClass{article} \let\contactrow\@gobble \def\contact#1{% \ifx\contactrow\@gobble\def\contactrow{Contact&}\fi \expandafter\def\expandafter\contactrow\expandafter{\contactrow&#1}} \let\addressrow\@gobble \def\address#1{% \ifx\addressrow\@gobble\def\addressrow{Your Address&}\fi \expandafter\def\expandafter\addressrow\expandafter{\addressrow&#1}} \let\phonerow\@gobble \def\phone#1{% \ifx\phonerow\@gobble\def\phonerow{Phone&}\fi \expandafter\def\expandafter\phonerow\expandafter{\phonerow&#1}} \let\emailrow\@gobble \def\email#1{% \ifx\emailrow\@gobble\def\emailrow{E-mail&}\fi \expandafter\def\expandafter\emailrow\expandafter{\emailrow&#1}} \def\makeheader{% \begin{center} \footnotesize \begin{tabular}{r*{20}{l}} \contactrow\\ \addressrow\\ \phonerow\\ \emailrow\\ \end{tabular}% \end{center}} 
6
  • Your last suggestion requires knowledge on how wide the table is. If the user has no idea, then the document wouldn't compile. Maybe I could specify the table number of columns somehow? Commented Oct 22, 2015 at 10:33
  • @aaragon no, it doesn't. (that is the person who puts \address{my address} in a document does not need to know.) Commented Oct 22, 2015 at 11:35
  • But if you put more & than required, the compilation won't take place, will it? Commented Oct 22, 2015 at 11:37
  • @aaragon the user of the document does not put in any &. As I mentioned above, if you had made a skeleton document in the question, there would have been a working document in the answer, MWE help all round really. Commented Oct 22, 2015 at 11:42
  • MWE provided... Commented Oct 22, 2015 at 12:39

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.