Defining an alias in LaTeX

4,703 views
Skip to first unread message

peter

unread,
Nov 21, 2011, 9:10:10 AM11/21/11
to LaTeX Users Group
Hi,

This is a very, very basic question, yet all the solutions I found so
far are somewhat unsatisfying.

I would like to define an alias, which I would afterward use like:

"The mechanism \ref{flow_topography_interaction} is the most effective
one when..."

with \ref{flow_topography_interaction} being replaced by another
string I defined earlier.

The answer I see everywhere is to "define a new command" as in:

\newcommand{\mycommand}{whatever I want}

But I do not find this terribly satisfying because underscores,
numbers and hyphens are not allowed within the name of commands. I can
hardly believe that there isn't a simpler and more elegant way in
LaTeX than to define a new function?

Thanks for any help..

Ariovaldo V Garcia

unread,
Nov 21, 2011, 11:50:43 AM11/21/11
to latexus...@googlegroups.com
Hi

I don't know if I understood quite well what you want. Maybe you can use \newsavebox  \sbox  and \usebox to get what you want. 

Something like this:


%defining a box variable
\newsavebox{\my_text}

...

%defining your text box
\sbox{\my_text}{my text that will appear}

And then using it where you want..

%Using the box:

The mechanism \usebox{\my_text} is the most effective one when...


Regards

Ariovaldo



--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.


peter

unread,
Nov 21, 2011, 12:22:28 PM11/21/11
to LaTeX Users Group
Thanks for the suggestion!
Indeed it works, but it has the same limitations as defining a new
command.
For instance, if I type:

> %defining a box variable
> \newsavebox{\my_text}

then LaTeX will struggle on the underscore.
Of course I could use "MyText" instead of "my_text" to avoid this
problem.

Another issue that I haven't mentioned is that the .tex file has to be
acceptable for a scientific journal.
Often, redefining commands or defining new commands are not allowed.

An ugly workaround would be to run a shell script that replaces the
alias with what I want.
I would call the shell script within my makefile, before the latex
compilation begins.
But I was hoping there would be a simple way of doing this within the
scope of latex.

Thanks again for the help.

Peter Flynn

unread,
Nov 21, 2011, 5:24:33 PM11/21/11
to latexus...@googlegroups.com
On Mon, Nov 21, 2011 at 2:10 PM, peter <pierr...@globetrotter.net> wrote:
Hi,

This is a very, very basic question, yet all the solutions I found so
far are somewhat unsatisfying.

I would like to define an alias, which I would afterward use like:

"The mechanism \ref{flow_topography_interaction} is the most effective
one when..."

with \ref{flow_topography_interaction} being replaced by another
string I defined earlier.

I'm afraid I don't understand what you want to do. Can you provide a complete example?
 
The answer I see everywhere is to "define a new command" as in:

\newcommand{\mycommand}{whatever I want}

But I do not find this terribly satisfying because underscores,
numbers and hyphens are not allowed within the name of commands. I can
hardly believe that there isn't a simpler and more elegant way in
LaTeX than to define a new function?

I am deducing from this that you have tried to define \flow_topography_interaction to do something, but you don't describe what it is you want to do.

The rules are simple: control sequences (commands) can ONLY contain letters, nothing else. If you simply want an alias for \ref{flow_topography_interaction} then I suggest

\newcommand{\fti}{\ref{flow_topography_interaction}}

or similar. Then use \fti where you need it. If you want lots of these, make a macro to define them more easily:

\documentclass{article}
\newcommand{\Ref}[2]{%
  \ref{#2}\expandafter\def\csname#1\endcsname{\ref{#2}}}
\begin{document}
\section{foo}
We will argue in \S~\Ref{fti}{flow_topography_interaction}
\section{Flow Topography Interaction}
\label{flow_topography_interaction}
Stuff
\section{bar}
As we saw in \S~\fti
\end{document}

With this, you use \Ref{fti}{flow_topography_interaction} the first time, and then subsequently you can use just \fti.

But this is guesswork: I'm still not clear if this is what you are looking for.

///Peter

peter

unread,
Nov 21, 2011, 6:00:21 PM11/21/11
to LaTeX Users Group

Thanks for the answer, and sorry about the confusion.
What I was looking for, is a way to associate a latex tag with a
string.
For instance, in my .tex file, I'd write something like:

"It was a (tag) night..."

and at compilation time, latex would replace the tag by a given
string:

"It was a dark and stormy night..."

That's all. As you mentioned, one way to do this is to define a new
command:

\newcommand{\MyTag}{dark and stormy}

and then use it as:

"It was a \MyTag night..."

However, the journal to which the .tex file will be sent does not
allow me to define new latex commands.
So I'm looking for alternate solutions. The one from Ariovaldo
(involving sbox and usebox) works and I will probably end up using
it.

Gildas Cotomale

unread,
Nov 21, 2011, 7:10:34 PM11/21/11
to latexus...@googlegroups.com
> An ugly workaround would be to run a shell script that replaces the
> alias with what I want.

I don't know why you do need that, but this feature may be provided by
your editor : search it's manual for "abbreviation" and "tag
expansion"... If you often use the same shortcuts/abbreviations, it's
good to manage them from your usual-favorite editor ; but you may also
save your pairs of texts in a file you can use for external processing
or inclusion (inclusion cons. is you are limited to your input-file
syntax, TeX here, but it doesn't matter if you don't have to use the
file elsewhere)

> I would call the shell script within my makefile, before the latex
> compilation begins.

As it's a shell (or other) script it can call latex commands after sed
or whatever you use...

> But I was hoping there would be a simple way of doing this within the
> scope of latex.
>

Maaybe, maybe not. Most of us can't give you the desired answer
because we never have that need and never hear about that. If LaTeX
can't do such thing then you'll need to go deeper into TeX internals
i'm afraid.

jon

unread,
Nov 21, 2011, 8:33:36 PM11/21/11
to LaTeX Users Group
On Nov 21, 6:00 pm, peter <pierrest...@globetrotter.net> wrote:
> However, the journal to which the .tex file will be sent does not
> allow me to define new latex commands.
> So I'm looking for alternate solutions. The one from Ariovaldo
> (involving sbox and usebox) works and I will probably end up using
> it.

why would they not allow newcommands but allow (new) saveboxes?
(just curious.)

cheers,
jon.

Ole Peter Smith

unread,
Nov 21, 2011, 8:37:00 PM11/21/11
to latexus...@googlegroups.com
Intereseting! Does latex permit newcommand's in the body? if not, why?

0le

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.




--
                                            /////
                                         ( O O )
=================oOO==(_)==OOo=================
      Humildadade, Ética e Sabedoria nunca subtraiam!
Pensar livremente é ser consciente dos seus paradigmas - 0le
                                .oooO   Oooo.
==================(     )=(     )=====================
                                     \  (     )  /
                                      \_)   (_/
===============================================
                        Ole Peter Smith, IME, UFG
    http://www.mat.ufg.br/docentes/olepeter - ole at mat.ufg.br
===============================================
                   Life sure is a Mystery to be Lived
                      Not a Problem to be Solved
===============================================

peter

unread,
Nov 22, 2011, 8:37:58 AM11/22/11
to LaTeX Users Group
Gildas:

> this feature may be provided by
> your editor : search it's manual for "abbreviation" and "tag
> expansion"...

> As it's a shell (or other) script it can call latex commands after sed

You are right, I could do this within vim, or simply call sed within
the makefile.
Those are useful suggestions. Thanks!

jon:

> why would they not allow newcommands but allow (new) saveboxes?

You raise a good point. I bet they don't allow new saveboxes...

Peter Flynn

unread,
Nov 22, 2011, 4:07:05 PM11/22/11
to latexus...@googlegroups.com
On Mon, Nov 21, 2011 at 5:22 PM, peter <pierr...@globetrotter.net> wrote:
Another issue that I haven't mentioned is that the .tex file has to be acceptable for a scientific journal.
Often, redefining commands or defining new commands are not allowed 
[...]
The journal to which the .tex file will be sent does not allow me to define new latex commands.

Ah. Now we come to the real reason :-)

Publishers hate, hate, hate authors who define their own commands, and (I'm sorry to say) with good reason. Perhaps 95% of all LaTeX files I receive for typesetting are broken because of author-created commands, missing private style files, packages obsolete for a decade, and an astonishing variety of private hacks, one author even redefining the \ command character to ! so that he could use \ for a real backslash (needless to say this broke the file fatally).

It's very unfair on people like us who take care to create robust and reusable macros that work without destroying the document, but it's the price we have to pay for the publishers accepting LaTeX documents. Don't forget, they apply their own private style files (in addition to their public classes) in order to get the document typeset, and they don't want their macros conflicting with ones you have invented.

None of which excuses the publisher's ignorance of LaTeX (with a few exceptions).
 
jon wrote:
why would they not allow newcommands but allow (new) saveboxes?
(just curious.)

Because \newsavebox is a built-in command. Creating a new savebox is in fact  just as risky for them as allowing \newcommand, but perhaps they don't know enough LaTeX to understand the difference.


Ole wrote:
Does latex permit newcommand's in the body?

Yes, although it's bad practice because it means a new user of the document might not see them, because \newcommands are meant to be all together at the top.

Ultimately, though, this thread is about something different to the normal author \newcommand, which often creates new formatting objects that may have unpleasant side-effects. Here, it's about simple text-replacement macros, which should be harmless but it's impossible to tell what else they might conflict with once the publisher's typesetter starts work.

You can certainly use editor macros (unsurprisingly, Emacs is especially good at this), but if you need this kind of facility in the very long term the real answer is not to author in raw LaTeX but use something like XML. That way you can define your own macros, and convert the whole thing to perfectly plain LaTeX to give to the publisher, using XSLT to do all the messy stuff about resolving the cross-references:

<!DOCTYPE doc [
<!ELEMENT doc (para|figure)*>
<!ELEMENT para (#PCDATA|xref)*>
<!ELEMENT xref EMPTY>
<!ATTLIST xref target IDREFS #REQUIRED>
<!ELEMENT figure (caption,graphic+)>
<!ATTLIST figure id ID #REQUIRED>
<!ELEMENT caption (#PCDATA)>
<!ELEMENT graphic EMPTY>
<!ATTLIST graphic fileref CDATA #REQUIRED>
<!ENTITY fti "flow_topography_interaction">
]>
<doc>
  <para>Some kind of text which refers to <xref
      target="&fti;"/> which occurs somewhere else.</para>
  <figure id="flow_topography_interaction">
    <caption>Flow Topography Interaction</caption>
    <graphic fileref="image.png"/>
  </figure>
  <para>And something else which refers to the same object (<xref
      target="&fti;"/>).</para>
</doc>

But in fact, if you use a decent XML editor, you never see this stuff, as it will assign an ID (label) to the figure when you first refer to it, and subsequently you just pick that figure off a menu when you want to refer to it, so you never see the label (or need to). But that kind of thing isn't easily available yet without spending a lot of money (or learning XML and XSLT and doing it by hand, in which case it would be easier to type &fti; than the whole string each time, I guess).

///Peter

peter

unread,
Nov 22, 2011, 5:07:00 PM11/22/11
to LaTeX Users Group
Thanks for all your suggestions, they were helpful!

I tried the solution from Gildas this afternoon (making substitutions
with sed) and I like it.
In my makefile I add these lines

cp $(texfile).tex /tmp/
sed -i -s "s/tag1/what should replace tag1/g" /tmp/$(texfile).tex
sed -i -s "s/tag2/what should replace tag2/g" /tmp/$(texfile).tex
etc.
and then it calls latex for the compilation of /tmp/$(texfile).tex

This is probably not the most elegant solution in the world, but I'm
very happy with it.

A great thanksgiving to you all...

Pierre

Gildas Cotomale

unread,
Nov 22, 2011, 7:57:54 PM11/22/11
to latexus...@googlegroups.com
>> this feature may be provided by
>> your editor : search it's manual for "abbreviation" and "tag
>> expansion"...
>
>> As it's a shell (or other) script it can call latex commands after sed
>
> You are right, I could do this within vim, or simply call sed within
> the makefile.
> Those are useful suggestions. Thanks!
>
An evil user :) (joke inside: "vi" is roman for 6, and for R. M.
Stallman --one of Emacs' father-- if you call it 3 times you have the
number 666) I am vi-addict too
":ab" is your friend <http://www.netadmintools.com/art243.html> As you
use Vim, ":h :ab" to learn more about it :)
Bonux: you may tweak your .vimrc to your abbreviations in a file
<http://vim.wikia.com/wiki/Using_abbreviations#Automatically_add_abbreviations_in_a_file>
What is good with having a separate database file is that you can use
it some other programs or editor <http://www.eskimo.com/~oak/abr/>


> I tried the solution from Gildas this afternoon (making substitutions
> with sed) and I like it.
> In my makefile I add these lines
>
> cp $(texfile).tex /tmp/
> sed -i -s "s/tag1/what should replace tag1/g" /tmp/$(texfile).tex
> sed -i -s "s/tag2/what should replace tag2/g" /tmp/$(texfile).tex
> etc.
> and then it calls latex for the compilation of /tmp/$(texfile).tex
>
> This is probably not the most elegant solution in the world, but I'm
> very happy with it.
>

You always have to start somewhere somehow :) Maybe next step with sed
is a replacements-file that can be used with "sed -f"
<http://www.linuxask.com/questions/replace-multiple-strings-using-sed>
either from your makefile or for any non-LaTeX file that need the same
processing? (For an independent file-format --e.g. tab/coma/colon
delimited-- you'll need a language that can directly process files
--awk for example-- or can read lines into array --like python, php,
perl, and so) Anyway it's good you have a solution that satisfy you.

Gildas Cotomale

unread,
Nov 22, 2011, 8:07:11 PM11/22/11
to latexus...@googlegroups.com
> Publishers hate, hate, hate authors who define their own commands, and (I'm
> sorry to say) with good reason. Perhaps 95% of all LaTeX files I receive for
> typesetting are broken because of author-created commands, missing private
> style files, packages obsolete for a decade, and an astonishing variety of
> private hacks, one author even redefining the \ command character to ! so
> that he could use \ for a real backslash (needless to say this broke the
> file fatally).
>
And when authors ask here, they often discover either a package that
does the job (and maybe they knew that package but as they didn't read
the manual...) or a good way to do things and best practices (as some
changes are really bad typography practices they didn't know)

> It's very unfair on people like us who take care to create robust and
> reusable macros that work without destroying the document, but it's the
> price we have to pay for the publishers accepting LaTeX documents. Don't
> forget, they apply their own private style files (in addition to their
> public classes) in order to get the document typeset, and they don't want
> their macros conflicting with ones you have invented.
>

And we can't help agreeing with publishers then.

Reply all
Reply to author
Forward
0 new messages