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.
LyX ⇒ User variables
NEW: TikZ book now 40% off at Amazon.com for a short time.

- Stefan Kottwitz
- Site Admin
- Posts: 10345
- Joined: Mon Mar 10, 2008 9:44 pm
User variables
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
and use in your document
Stefan
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.}
\customer
as TeX code.Stefan
LaTeX.org admin
User variables
Hi Stefan,Stefan_K wrote:and use in your documentCode: Select all
\newcommand*{\customer}{ACME Ltd.}\customer
as TeX code.
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:
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.ACME Ltd.written at the beginning, ACME Ltd., the middle and the end of a line ACME Ltd..
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:
That output is much better as it resolves the issues I described. Is there a better way to accomplish the same result?ACME Ltd. written at the beginning, ACME Ltd., the middle and the end of a line ACME Ltd.
Marc.
User variables
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
Although there exists the xspace package which tries to allow input like
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
Regards
\
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 ...
Code: Select all
\customer written at the beginning ...
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
Code, edit and compile here:
\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 aline ACME Ltd.% right spacing:ACME Ltd.\@ written at the beginning, ACME Ltd., the middle and the end of aline ACME Ltd.% automated:\customer written at the beginning, \customer, the middle and the end of aline \customer.\end{document}
site moderator & package author
User variables
Clemens,cgnieder wrote:Code: Select all
Code, edit and compile here:\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 aline ACME Ltd.% right spacing:ACME Ltd.\@ written at the beginning, ACME Ltd., the middle and the end of aline ACME Ltd.% automated:\customer written at the beginning, \customer, the middle and the end of aline \customer.\end{document}
Thank you very much, that is exactly what I was looking for!
Marc.