LaTeX forum ⇒ GeneralAutomatic capitalization within a macro?

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

Automatic capitalization within a macro?

Postby olly78 » Wed Sep 24, 2008 11:12 am

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

User avatar
Juanjo
Posts: 656
Joined: Sat Jan 27, 2007 12:46 am

Postby Juanjo » Wed Sep 24, 2008 6:41 pm

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

Postby olly78 » Wed Sep 24, 2008 8:18 pm

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
Location: Columbus, OH
Contact:

Postby Ted » Wed Sep 24, 2008 10:50 pm

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)...
  1. \usepackage{varioref}
  2. \labelformat{equation}{\textup{(#1)}}
  3. \usepackage{hyperref}
  4.  
  5. \let\orgautoref\autoref
  6. \providecommand{\Autoref}
  7. {\def\equationautorefname{Equation}%
  8. \def\figureautorefname{Figure}%
  9. \def\subfigureautorefname{Figure}%
  10. \def\Itemautorefname{Item}%
  11. \def\tableautorefname{Table}%
  12. \def\sectionautorefname{Section}%
  13. \def\subsectionautorefname{Section}%
  14. \def\subsubsectionautorefname{Section}%
  15. \def\chapterautorefname{Section}%
  16. \def\partautorefname{Part}%
  17. \orgautoref}
  18.  
  19. \renewcommand{\autoref}
  20. {\def\equationautorefname{equation}%
  21. \def\figureautorefname{figure}%
  22. \def\subfigureautorefname{figure}%
  23. \def\Itemautorefname{item}%
  24. \def\tableautorefname{table}%
  25. \def\sectionautorefname{section}%
  26. \def\subsectionautorefname{section}%
  27. \def\subsubsectionautorefname{section}%
  28. \def\chapterautorefname{section}%
  29. \def\partautorefname{part}%
  30. \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

Postby olly78 » Thu Sep 25, 2008 12:52 pm

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
Location: Columbus, OH
Contact:

Postby Ted » Thu Sep 25, 2008 3:21 pm

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

Postby olly78 » Thu Sep 25, 2008 7:37 pm

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.


Return to “General”

Who is online

Users browsing this forum: Bing [Bot] and 10 guests