LyX ⇒ User variables
User variables
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.
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
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
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
\
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
\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}
User variables
Clemens,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}
Thank you very much, that is exactly what I was looking for!
Marc.