LyXUser variables

Information and discussion about LyX, a WYSIWYM editor, available for Linux, Windows and Mac OS X systems.
Post Reply
mvw
Posts: 5
Joined: Fri Jul 12, 2013 3:11 am

User variables

Post by mvw »

Hi,

I am utterly new to LyX, coming from an MS-Word environment. I am looking to use variable in a document, but am not sure how to best do this.

I write a lot of documents in which the name of a particular client is often used. For example:

"We like to thank ACME Ltd. to provide us with the opportunity to quote on this project. ACME Ltd. has been a customer of ours for many years and we look forward to further strengthen our relationship with ACME Ltd."

Rather than constantly writing "ACME Ltd.", could I use a variable? A lot of the text is the same thoughout my documents, so I could then write it once and save it as a template. The next customer comes along and I simply change the value of the variable to the next customer's name.

Any ideas?

Marc.

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

User variables

Post by Stefan Kottwitz »

Hi Marc,

welcome to the forum!

Sure, that's a basic feature of LaTeX, wich is very important: macros. You can define your own macros. For example, you could write in your document preamble

Code: Select all

\newcommand*{\customer}{ACME Ltd.}
and use in your document \customer as TeX code.

Stefan
LaTeX.org admin
mvw
Posts: 5
Joined: Fri Jul 12, 2013 3:11 am

User variables

Post by mvw »

Stefan_K wrote:

Code: Select all

\newcommand*{\customer}{ACME Ltd.}
and use in your document \customer as TeX code.

Stefan
Hi Stefan,

Thank you for your response. I have added your code to the document preamble and wrote some text:

\customer written at the beginning, \customer, the middle and the end of a line \customer.

I find that the output then looks like this:
ACME Ltd.written at the beginning, ACME Ltd., the middle and the end of a line ACME Ltd..
I am not happy with the way that looks. First there is no space between the first instance of ACME Ltd. and the word "written". Second, the line ends in ".."; the first dot belonging to "ACME Ltd." and the second one to finish the end of the sentence.

I have been poking around a bit more and found that when I write the following text, the output is the way I want:

\customer\ written at the beginning, \customer, the middle and the end of a line \customer

Note that I added a backslash at the end of the first TeX code and omitted the dot at the end of the sentence. The output now looks like this:
ACME Ltd. written at the beginning, ACME Ltd., the middle and the end of a line ACME Ltd.
That output is much better as it resolves the issues I described. Is there a better way to accomplish the same result?

Marc.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

User variables

Post by cgnieder »

It is standard behaviour that a macro (at least one whose name consists of letters) swallows all following spaces until the next non-space. The standard (recommended) approach is what you've detected for yourself: add the space explicitly by calling \ after it. You can also insert an empty group after the macro:

Code: Select all

\customer\ written at the beginning ...
\customer{} written at the beginning ...
Although there exists the xspace package which tries to allow input like

Code: Select all

\customer written at the beginning ...
and still gives the space I wouldn't recommend it as a possible failure is much harder to predict and detect. I'll use it below anyway...

As for the doubling of the point: we can try to detect a following point and add the point after “Ltd” only if there is none. This can be done with the help of the LaTeX kernel macro \@ifnextchar<char>{<true>}{<false>}. Also, should you be using non-frenchspacing in your document you should add the macro \@ after the point. If you don't do this LaTeX will interpret the point as the end of a sentence which means that the following inter-word space will actually become an inter-sentence space. With non-frenchspacing this is slightly larger.

Code: Select all

\documentclass{article}

% provides the \xspace macro that in most cases correctly adds a
% space after the macro when there should be one:
\usepackage{xspace}
% make @ a letter so we can use it in macro names:
\makeatletter
% define the custom command:
\newcommand*{\customer}{ACME Ltd\@ifnextchar.{}{.\@\xspace}}
% make @ other again:
\makeatother

\begin{document}

% wrong spacing:
ACME Ltd. written at the beginning, ACME Ltd., the middle and the end of a
line ACME Ltd.

% right spacing:
ACME Ltd.\@ written at the beginning, ACME Ltd., the middle and the end of a
line ACME Ltd.

% automated:
\customer written at the beginning, \customer, the middle and the end of a
line \customer. 

\end{document}
customer.png
customer.png (17.2 KiB) Viewed 8804 times
Regards
site moderator & package author
mvw
Posts: 5
Joined: Fri Jul 12, 2013 3:11 am

User variables

Post by mvw »

cgnieder wrote:

Code: Select all

\documentclass{article}

% provides the \xspace macro that in most cases correctly adds a
% space after the macro when there should be one:
\usepackage{xspace}
% make @ a letter so we can use it in macro names:
\makeatletter
% define the custom command:
\newcommand*{\customer}{ACME Ltd\@ifnextchar.{}{.\@\xspace}}
% make @ other again:
\makeatother

\begin{document}

% wrong spacing:
ACME Ltd. written at the beginning, ACME Ltd., the middle and the end of a
line ACME Ltd.

% right spacing:
ACME Ltd.\@ written at the beginning, ACME Ltd., the middle and the end of a
line ACME Ltd.

% automated:
\customer written at the beginning, \customer, the middle and the end of a
line \customer. 

\end{document}
Clemens,

Thank you very much, that is exactly what I was looking for!

Marc.
Post Reply