GeneralAutomatic capitalization within a macro?

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Automatic capitalization within a macro?

Post by olly78 »

Hi

I use the following two macros to generate references to figures (I do the same for equations, etc.):
\def\fgr#1{figure~\ref{fig:#1}}
\def\Fgr#1{Figure~\ref{fig:#1}}

This allows me to change the format easily to, e.g. to fig. 1 or figure (1). However, I need to use the second macro if the reference is at the start of a sentence.

It would be great to have only one macro, which works out whether the reference is at the start of a sentence, and capitalizes accordingly. Any ideas?

Many thanks,
Olly

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Automatic capitalization within a macro?

Post by Juanjo »

I think it is good style to always capitalize the word figure when it is followed by a number, since it then behaves as a proper noun. This is true, at least, for publishing in many mathematical journals. I would say, for example, "... as shown in Figure 3". So, in my opinion, you only need one macro. Anyway, you will find interesting the features of the varioref package.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Re: Automatic capitalization within a macro?

Post by olly78 »

I don't disagree about always capitalizing "Figure", but sometimes I'm specifically asked to use upper or lower case depending on context.

The varioref package is interesting - thanks for pointing me to it. Unfortunately it suffers from the same problem I've got, which is that you need to use \Vref to capitalize the first letter, and \vref otherwise.

Is there a macro which is true if called at the start of a sentence, and false otherwise? I could probably work the rest out.
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Automatic capitalization within a macro?

Post by Ted »

olly78 wrote:I don't disagree about always capitalizing "Figure", but sometimes I'm specifically asked to use upper or lower case depending on context.
Very often journals have style guidelines that fix this. These issues are rarely up to the author when publishing through someone else.
olly78 wrote:The varioref package is interesting - thanks for pointing me to it. Unfortunately it suffers from the same problem I've got, which is that you need to use \Vref to capitalize the first letter, and \vref otherwise.
Personally, I use \autoref from the hyperref package. I combine it with varioref... I do a \labelformat{equation}{\textup{(#1)}} and then use \autoref{eq:blah} which gets replaced by "Equation~(1)" (while \autoref{tab:blah} gets replaced by "Table~1").
olly78 wrote:Is there a macro which is true if called at the start of a sentence, and false otherwise? I could probably work the rest out.
How do you determine what IS the "start" of a sentence? Do you back-track through all of the previous glue or white space until you find a period? And what if that period is ending an abbreviation and not a sentence?

You've managed to teach yourself to capitalize your *own* text at the start of each sentence. Just train yourself to capitalize your macros too. Then use something like (at the END of your preamble)...

Code: Select all

\usepackage{varioref}
\labelformat{equation}{\textup{(#1)}}
\usepackage{hyperref}

\let\orgautoref\autoref
\providecommand{\Autoref}
        {\def\equationautorefname{Equation}%
         \def\figureautorefname{Figure}%
         \def\subfigureautorefname{Figure}%
         \def\Itemautorefname{Item}%
         \def\tableautorefname{Table}%
         \def\sectionautorefname{Section}%
         \def\subsectionautorefname{Section}%
         \def\subsubsectionautorefname{Section}%
         \def\chapterautorefname{Section}%
         \def\partautorefname{Part}%
         \orgautoref}

\renewcommand{\autoref}
        {\def\equationautorefname{equation}%
         \def\figureautorefname{figure}%
         \def\subfigureautorefname{figure}%
         \def\Itemautorefname{item}%
         \def\tableautorefname{table}%
         \def\sectionautorefname{section}%
         \def\subsectionautorefname{section}%
         \def\subsubsectionautorefname{section}%
         \def\chapterautorefname{section}%
         \def\partautorefname{part}%
         \orgautoref}
You could define \Autorefs and \autorefs in the same way.

The cleveref package is an alternative to \autoref that's a bit smarter. It gives you a little more automation and customizability.
-- Ted [home/blog]
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Automatic capitalization within a macro?

Post by olly78 »

Ted wrote:Personally, I use \autoref from the hyperref package. I combine it with varioref... I do a \labelformat{equation}{\textup{(#1)}} and then use \autoref{eq:blah} which gets replaced by "Equation~(1)" (while \autoref{tab:blah} gets replaced by "Table~1").
That is definitely more straightforward than my multiple macros for different reference types. Thanks.
Ted wrote:You've managed to teach yourself to capitalize your *own* text at the start of each sentence. Just train yourself to capitalize your macros too.
This is what I'm doing currently. It's relatively easy to rejig a sentence and forget to capitalize/uncapitalize the macro, though. However, you're right in saying that it's the same problem with normal text. I was just looking for a more elegant solution.
Ted wrote:How do you determine what IS the "start" of a sentence? Do you back-track through all of the previous glue or white space until you find a period? And what if that period is ending an abbreviation and not a sentence?
I don't know much about the inner workings of LaTeX, unfortunately. However, I know that it can make spaces after full stops longer (you can avoid this for abbreviations by using "\ " for the following space, I think), and also that it can indent the start of paragraphs. These are the only two situations you'd want the automatic capitalization (that I can think of), so if LaTeX knows it's in one of these two situations then logic tells me it should be possible to create a macro that tells you this also. However, I've now come across lots of macro packages (including the ones mentioned in this thread) which have the same problem, and all solve it the same way I was - i.e. have one macro for start of sentences, and another for the body, so it seems likely that such a macro doesn't currently exist. I'm happy to leave it at that, and keep doing what I was doing before.
Ted
Posts: 94
Joined: Sat Jun 23, 2007 4:11 pm

Automatic capitalization within a macro?

Post by Ted »

olly78 wrote:
Ted wrote:Personally, I use \autoref from the hyperref package. I combine it with varioref... I do a \labelformat{equation}{\textup{(#1)}} and then use \autoref{eq:blah} which gets replaced by "Equation~(1)" (while \autoref{tab:blah} gets replaced by "Table~1").
That is definitely more straightforward than my multiple macros for different reference types. Thanks.
Again, the cleveref package uses a similar mechanism (it plays with refstepcounter) to have one macro take care of all of your referencing. Some people like its approach better than \autoref, but many people find that autoref is perfect for their needs (and supported by journal publishers who haven't updated their TeX distros in a long while).
olly78 wrote:
Ted wrote:How do you determine what IS the "start" of a sentence? Do you back-track through all of the previous glue or white space until you find a period? And what if that period is ending an abbreviation and not a sentence?
I don't know much about the inner workings of LaTeX, unfortunately.
TeX is a parser. It's nothing more. It has a context-free grammar that it uses to churn through its input. Each new character changes the state of the machine, and that causes it to act differently. Indentation is pretty easy to handle and to short-circuit, but capitalization is trickier. Giving TeX the ability to detect where capitalization is appropriate would require more machinery than what you can pack into a package.

Also keep in mind that figure labels often change more than just in capitalization at the start of a sentence. Most style guides say that if you use "fig.~1" in the middle of a sentence, you should use "Figure~1" at the start of a sentence because you don't want to put the one period so close to the previous one. So somewhere you still need something specifying mid-sentence and start-of-sentence expansions. It doesn't just come down to capitalizing the first letter; it comes down to switching from one macro to the next.

So I think when you weigh all of the pros and cons of having automated capitalization (or automated hooks that respond to some "start-of-sentence-detected" flag that is probably often wrong), it's just easier and less error prone to do it manually. Plus, because it makes you think about the text more, it probably makes for better writing too.
-- Ted [home/blog]
olly78
Posts: 14
Joined: Wed Sep 24, 2008 10:57 am

Automatic capitalization within a macro?

Post by olly78 »

Ted wrote:Giving TeX the ability to detect where capitalization is appropriate would require more machinery than what you can pack into a package.
I'll take that as a definitive answer. Many thanks for your help, Ted.
Post Reply