GeneralCreate text string commands for forms?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
pgeezer
Posts: 7
Joined: Wed Dec 08, 2010 4:57 pm

Create text string commands for forms?

Post by pgeezer »

Hello,

I'd like to be able to set strings as commands at the top of a doc, so that one can enter all the pertinent info at the top of the doc and then insert those strings into a preformatted document.

EG:
\newcommand{\Author}{pgeezer}

This document was created by \Author .
and have "pgeezer" auto-replace the \Author command. I tried the commands above and it doesn't work. Is there another way to do this?
Last edited by pgeezer on Fri Dec 10, 2010 12:25 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Create text string commands for forms?

Post by localghost »

"Doesn't work" is one of the most trivial statements and doesn't contain any useful information. You have to be more specific what does not work and if there are any error messages. Moreover it is most helpful to always post a minimal example. The code as is works fine for me. But there is a more elegant way to do this.

Code: Select all

\documentclass[11pt,a4paper,english]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}

\title{Creating text string commands for forms}
\author{pgeezer}

\makeatletter
\let\theauthor\@author
\let\thetitle\@title
\makeatother

\begin{document}
  \maketitle

  This document titled ``\thetitle'' was created by \theauthor.
\end{document}

Best regards and welcome to the board
Thorsten
pgeezer
Posts: 7
Joined: Wed Dec 08, 2010 4:57 pm

Create text string commands for forms?

Post by pgeezer »

Hi Thorsten,

Thanks for welcoming me. I'm struggling a little with learning latex and couldn't describe the way in which it output my first example. Sometimes it output the variable I defined as a text string, and sometimes it simply output a number instead of my text string (maybe after the ninth one?). Additionally, I got errors saying that:
0certs.tex(16): Error: ! You already have nine parameters.
0certs.tex(18): Error: ! Missing number, treated as zero.
0certs.tex(18): Error: ! You already have nine parameters.
0certs.tex(19): Error: ! Missing number, treated as zero.
0certs.tex(19): Error: ! You already have nine parameters.
I was so confused all I could put was "it didn't work".

Are the variables you put in code working because they are part of the template or can I define one as:

Code: Select all

\qbf{the quick brown fox jumped over the lazy dog}

My favourite test phrase to use in determining how fonts look is \qbf .
Will this work for any text string variable or am I thinking of this wrong? I wish to build a list of newly-defined text strings at the beginning of the doc and use them to automatically put those strings in the document.

One text string might be defined as \uniaddress or somesuch. I wish to define it once at the top of the document, preformatted, and have the command insert "The University of Western Australia, Perth, WA" for me anywhere I type \uniaddress .
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Create text string commands for forms?

Post by frabjous »

You need to follow Thorsten's advice and post an actual minimal example that generates the kinds of errors you're discussing. It is impossible to guess where such errors might be coming from otherwise. Another guide for creating a such a minimal document is here.

You can follow the procedure for any string of text, yes, but you need to use \newcommand (and be sure to use a command string not already defined)

Code: Select all

\documentclass{article}
\newcommand{\qbf}{the quick brown fox jumps over the lazy dog}
\begin{document}
My favorite string for testing documents is ``\qbf''.
\end{document}
Some possible sources of errors however is if you wanted the abbreviated string to be followed immediately by additional letters. You could not write "\qbfs" for "the quick brown fox jumps over the lazy dogs", because then LaTeX would think "s" was part of the command and it would tell you it was an undefined control sequence. Spaces immediately following \qbf are ignored, so you would use "\qbf s" instead. (If you do want a space, you'd need to use "\ " after it.)

This is off topic, but if you are going to use \qbf, I do suggest using "jumps" rather than "jumped" so all 26 letters are used; you're missing an s otherwise.

Thorsten's method for \theauthor and \thetitle make use of the \author and \title commands already defined as part of the document's \maketitle mechanism and are specific to things like that, and couldn't be used for arbitrary text, but the \newcommand method should work.
pgeezer
Posts: 7
Joined: Wed Dec 08, 2010 4:57 pm

Create text string commands for forms?

Post by pgeezer »

Ok thanks. I think I understand what a minimal example is now and will work on making one. I'm on the right track, but will make an isolated file to see better.

Am I limited to nine "\newcommand" s in a row? Here's the list with only the variables and definitions changed:

Code: Select all

% FILL IN THE FIELDS BELOW AND INSPECT OUTPUT:
\begin{document}

\newcommand{\CO}{Joe Schmo}
\newcommand{\COposition}{Senior Joe}
\newcommand{\COcompany}{Joe Widgets}
\newcommand{\COaddress}{Jones Street, Joeburg}
\newcommand{\COphone}{1-555-555-5555}
\newcommand{\COemail}{joe.schmo@joe.com}
\newcommand{\COdeg1}{Master of Lumber in Termitology}
\newcommand{\COdeg1university}{the University of Central Joeland
\newcommand{\COdeg1year}{2003}
\newcommand{\COdeg2}{Bachelor of Wood with Specialization in Termitology}
\newcommand{\COdeg2university}{the University of East Joeland}
\newcommand{\COdeg2year}{1997}

This is a bunch of text by \CO, \COposition of COcompany, with a \COdeg1 from \COdeg1year.  You can reach him by phone at \COphone,... <etc, etc>

\end{document}
Is there a limit to these? Should they be outside the document environment?
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Create text string commands for forms?

Post by frabjous »

pgeezer wrote:Ok thanks. I think I understand what a minimal example is now and will work on making one. I'm on the right track, but will make an isolated file to see better.
You're close, but you do need to include at least the \documentclass line so that the sample code can be compiled as is.

Am I limited to nine "\newcommand" s in a row? Here's the list with only the variables and definitions changed:
I don't think there's any limit like that. (Well, there is some upward limit based on brute memory limitations, but it far exceeds nine.)

The only problem I see with your example is that command names must consist of letters only, with no digits, so you cannot use "\COdeg1" "\COdeg2university", etc., since these have digits in them.

Oh, and you missed a closing brace at the end of "University of Central Joeland".

Code: Select all

\documentclass{article}
\newcommand{\CO}{Joe Schmo}
\newcommand{\COposition}{Senior Joe}
\newcommand{\COcompany}{Joe Widgets}
\newcommand{\COaddress}{Jones Street, Joeburg}
\newcommand{\COphone}{1-555-555-5555}
\newcommand{\COemail}{joe.schmo@joe.com}
\newcommand{\COdegone}{Master of Lumber in Termitology}
\newcommand{\COdegoneuniversity}{the University of Central Joeland}
\newcommand{\COdegoneyear}{2003}
\newcommand{\COdegtwo}{Bachelor of Wood with Specialization in Termitology}
\newcommand{\COdegtwouniversity}{the University of East Joeland}
\newcommand{\COdegtwoyear}{1997}

\begin{document}
This is a bunch of text by \CO, \COposition\ of \COcompany, with a \COdegone\ from \COdegoneyear.  You can reach him by phone at \COphone, \ldots

\end{document}
This should work. Notice a couple things. First, I moved these definitions up into the preamble. This isn't strictly necessary, but it's a good practice to avoid the linebreaks at the end of the lines from altering your document.

Also, I put an extra slash after "\COposition\ "; as I mentioned above, the space after a command name is gobbled, and without this you would lose that space "\ " (that is, backslash followed by a space) manually inserts a space back where you need it. (You could get around this with the xspace package, but let's not try to do too much at once.)
pgeezer
Posts: 7
Joined: Wed Dec 08, 2010 4:57 pm

Re: Create text string commands for forms?

Post by pgeezer »

Thanks for this. Next time I'll make the example compilable as per minimal working example.

The numbers were appearing because I had numbers in the variables. It would spit out the numbered variables, where defined, as a row of numbers and values. In the body text, the numbered variables just show up as numbers in the text, making me think it might have been doing something like making footnote definitions. I see now it was nothing of the sort. Taking the numbers out as you suggested makes everything work.

Also, thanks for the advice of putting an extra slash in to confirm a space. I was having trouble with that.

I've also moved the variables into the preamble as you suggested. I've even defined the headers and footers with new variables! Sweet. :)
Post Reply