21

I'm using the acronym package in my document and in several instances I want to start a sentence with the expanded acronym. Is there any way of telling the package to capitalise the first letter?

For example:

\acrodef{TLA}{three-letter acronym}
\acrodefplural{TLA}{three-letter acronyms}
...end of one sentence. \aclp{TLA} are acronyms with three letters...

This will yield:

...end of one sentence. three-letter acronyms are acronyms with three letters...

lockstep
  • 250,273
  • @Stefan That seems to be asking how to capitalise the first letter in the acronym list; I want a way to optionally capitalise the first letter of the expanded acronym when used in text (and I'm not sure how to adapt what was suggested in the answers for my purposes). I was hoping there'd be a family of commands like \Acl, \Acf, etc. that would do this, but it seems there isn't. (Maybe the author should introduce these?) – Will Vousden Nov 01 '12 at 16:47
  • Maybe you can use this: http://www.latex-community.org/forum/viewtopic.php?f=5&t=9966 – hpekristiansen Nov 02 '12 at 23:56

5 Answers5

9

Acronym 1.42 adds the \Ac{p,f,fp,fi,l,lp,lu} commands. They work exactly the same way as the \ac commands, but prints the full name, starting with an upper case letter (in case it was defined with a lower case letter). Thanks to Oz Davidi!

\Ac{TLA} at the beginning of a sentence instead of \ac{TLA} as normal.
9

Just a couple of macros, with no attempt to give the complete set:

\documentclass{article}
\usepackage{acronym}

\makeatletter
\newif\ifAC@uppercase@first
\def\Aclp#1{\AC@uppercase@firsttrue\aclp{#1}\AC@uppercase@firstfalse}
\def\AC@aclp#1{%
  \ifcsname fn@#1@PL\endcsname
    \ifAC@uppercase@first
      \expandafter\expandafter\expandafter\MakeUppercase\csname fn@#1@PL\endcsname
    \else
      \csname fn@#1@PL\endcsname
    \fi
  \else
    \AC@acl{#1}s
  \fi 
}
\edef\AC@uppercase@write{\string\ifAC@uppercase@first\string\expandafter\string\MakeUppercase\string\fi\space}
\def\AC@acrodef#1[#2]#3{%
  \@bsphack
  \protected@write\@auxout{}{%
    \string\newacro{#1}[#2]{\AC@uppercase@write #3}%
  }\@esphack
}
\def\Acl#1{\AC@uppercase@firsttrue\acl{#1}\AC@uppercase@firstfalse}
\makeatother

\acrodef{TLA}{three-letter acronym}
\acrodefplural{TLA}{three-letter acronyms}

\begin{document}
\Aclp{TLA} are acronyms with three letters.
\Acl{TLA} at the start of another sentence.
\end{document}

enter image description here

egreg
  • 1,121,712
7

This is a follow-up to Egreg's answer, basically the full set of \Ac?? commands. Two points to note first:

  1. Somewhere was a stray trailing space that affected \Acfp (and the first use of \Acp) - There was a space before the closing bracket. Oddly, it broke the original \acfp and \acp as well, which I'm sure to a finer mind than mine would indicate roughly where in the redefinitions it is. I just terminated pretty much all the lines with a % without paying any attention to what they did.

  2. Note the use of \robustify (from etoolbox), which is required if you want to use acronyms in headings (perhaps more likely now they can start with caps)

I don't pretend to understand the code, just that having done Egreg's "exercise for the reader" and found a couple of extra complexities along the way I thought I'd share.

% Extend acronym package with first letter caps
\makeatletter
\newif\ifAC@uppercase@first%
\def\Aclp#1{\AC@uppercase@firsttrue\aclp{#1}\AC@uppercase@firstfalse}%
\def\AC@aclp#1{%
  \ifcsname fn@#1@PL\endcsname%
    \ifAC@uppercase@first%
      \expandafter\expandafter\expandafter\MakeUppercase\csname fn@#1@PL\endcsname%
    \else%
      \csname fn@#1@PL\endcsname%
    \fi%
  \else%
    \AC@acl{#1}s%
  \fi%
}%
\def\Acp#1{\AC@uppercase@firsttrue\acp{#1}\AC@uppercase@firstfalse}%
\def\AC@acp#1{%
  \ifcsname fn@#1@PL\endcsname%
    \ifAC@uppercase@first%
      \expandafter\expandafter\expandafter\MakeUppercase\csname fn@#1@PL\endcsname%
    \else%
      \csname fn@#1@PL\endcsname%
    \fi%
  \else%
    \AC@ac{#1}s%
  \fi%
}%
\def\Acfp#1{\AC@uppercase@firsttrue\acfp{#1}\AC@uppercase@firstfalse}%
\def\AC@acfp#1{%
  \ifcsname fn@#1@PL\endcsname%
    \ifAC@uppercase@first%
      \expandafter\expandafter\expandafter\MakeUppercase\csname fn@#1@PL\endcsname%
    \else%
      \csname fn@#1@PL\endcsname%
    \fi%
  \else%
    \AC@acf{#1}s%
  \fi%
}%
\def\Acsp#1{\AC@uppercase@firsttrue\acsp{#1}\AC@uppercase@firstfalse}%
\def\AC@acsp#1{%
  \ifcsname fn@#1@PL\endcsname%
    \ifAC@uppercase@first%
      \expandafter\expandafter\expandafter\MakeUppercase\csname fn@#1@PL\endcsname%
    \else%
      \csname fn@#1@PL\endcsname%
    \fi%
  \else%
    \AC@acs{#1}s%
  \fi%
}%
\edef\AC@uppercase@write{\string\ifAC@uppercase@first\string\expandafter\string\MakeUppercase\string\fi\space}%
\def\AC@acrodef#1[#2]#3{%
  \@bsphack%
  \protected@write\@auxout{}{%
    \string\newacro{#1}[#2]{\AC@uppercase@write #3}%
  }\@esphack%
}%
\def\Acl#1{\AC@uppercase@firsttrue\acl{#1}\AC@uppercase@firstfalse}
\def\Acf#1{\AC@uppercase@firsttrue\acf{#1}\AC@uppercase@firstfalse}
\def\Ac#1{\AC@uppercase@firsttrue\ac{#1}\AC@uppercase@firstfalse}
\def\Acs#1{\AC@uppercase@firsttrue\acs{#1}\AC@uppercase@firstfalse}
\robustify\Aclp
\robustify\Acfp
\robustify\Acp
\robustify\Acsp
\robustify\Acl
\robustify\Acf
\robustify\Ac
\robustify\Acs
\makeatother

EDIT: If you want to define your acronyms using \acro in an acronym environment, the above code doesn't give you the caps, because the line that stores the acronym is in a different macro and doesn't get replaced with the rather neat definition that Egreg came up with.

The solution is to also patch (well, overwrite) acro:

\def\AC@@acro#1[#2]#3{%
  \ifAC@nolist%
  \else%
  \ifAC@printonlyused%
    \expandafter\ifx\csname acused@#1\endcsname\AC@used%
       \item[\protect\AC@hypertarget{#1}{\acsfont{#2}}] #3%
          \ifAC@withpage%
            \expandafter\ifx\csname r@acro:#1\endcsname\relax%
               \PackageInfo{acronym}{%
                 Acronym #1 used in text but not spelled out in
                 full in text}%
            \else%
               \dotfill\pageref{acro:#1}%
            \fi\\%
          \fi%
    \fi%
 \else%
    \item[\protect\AC@hypertarget{#1}{\acsfont{#2}}] #3%
 \fi%
 \fi%
 \begingroup
    \def\acroextra##1{}%
    \@bsphack
    \protected@write\@auxout{}%
       {\string\newacro{#1}[\string\AC@hyperlink{#1}{#2}]{\AC@uppercase@write #3}}%
    \@esphack
  \endgroup}

Edit 2: Note that this 2nd block needs to be inserted into the first block before the last (\makeatother) line.

Chris H
  • 8,705
  • This is ideal, thanks. Do you mind if I point one of the maintainers to this post so that it might be integrated into the package? – Will Vousden Dec 20 '13 at 15:40
  • @WillVousden Of course not, go ahead - but they may have a better way of building it in rather than patching. – Chris H Dec 20 '13 at 15:50
  • Of course, it would be nice to have a version that capitalises all first letters, or even some cleverer title case version, maybe even configurable. And it would be a good learning exercise for me. When I wouldn't be using it as an excuse for neglecting my thesis. – Chris H Jan 06 '14 at 09:02
  • This does not work for me. – alphabetasoup Mar 01 '15 at 19:48
  • @alpha-beta-soup, what happens? I suggest a new question to give you room to post an MWE. I haven't looked at this for a year now but should stitch together all my acronym mods into one file at some point. – Chris H Mar 01 '15 at 19:55
  • It does work if the \makeatother is removed. – alphabetasoup Mar 01 '15 at 19:58
  • @alpha-beta-soup, that's odd, I would say you must be defining another macro that include "@" later - we need to see the full minimum code that fails. – Chris H Mar 01 '15 at 20:00
  • Ah, I think I see now. I don't really understand TeX very well, I just hack my way through it until I'm satisfied. If I move \makeatother after the acro patch in my preamble, it works with the \makeatother. I should probably go find out what that command actually does. Thanks for your work on this. – alphabetasoup Mar 01 '15 at 20:03
  • @alpha-beta-soup, that makes sense - the line \def\AC@@acro#1[#2]#3{% (and some others) needs to be between \makeatletter and \makeateother. See http://tex.stackexchange.com/q/8351/28808 for more info. I'll update the answer to make this clearer. I got to this point by just hacking about as well, so don't let that worry you. – Chris H Mar 02 '15 at 14:33
  • Hi, I know its been a long time, but any idea how you mgiht implement this so you can define with \newacro instead of \acrodef? – Gilly May 23 '17 at 08:23
  • @Gilly I'll have a look. I'm interested in polishing this code so it's more widely useful so I should look at it again. – Chris H May 23 '17 at 08:31
  • @Gilly it's not as simple as I hoped, so I haven't got time now. Why do you need \newacroinstead of \acrodef? The only difference according to the manual is that the latter writes to the aux file, which is how this set of macros works. That means compiling twice but you normally need to do that anyway because of cross-referencing. – Chris H May 23 '17 at 09:12
  • @ChrisH Thank you very much for having a look. I am submitting to a Journal (using revtex4-1), and for some reason their documentclass conflicts with \acrodef. I have found a solution by acrodefing with a different document class and your solution, then copying the \newacro portion of the aux file into the tex file :) Not ideal, but more than fine for me since I am near completion and won't be adding new acronyms. Thanks very much for this code, by the way – Gilly May 23 '17 at 23:34
  • Looks like that for consistency with package built-in macros, both occurences of \acsfont{#2}in the 2nd block should be replaced with \aclabelfont{#2}. Otherwise, the way of controlling acronyms' short form format in the list of acronyms – by redefining \aclabelfont (as presented in acronym package manual) – would not work, e.g., \renewcommand*{\aclabelfont[1{\textbf{\textsf{\acsfont{#1}}}}. – Peter Feb 28 '18 at 19:36
  • @ChrisH There is a bug in this. I have some acronyms which contain a number in their key. By calling \ac, \acl or other built-in commands, everything works fine, when using \Ac or \Acl and so on, the document won't compile. As I do not really understand the code, I have no idea how to fix this, but I think that it would be great if it was fixed. – Sam Oct 27 '19 at 11:20
  • @ChrisH I also noticed, that the patch will just capitalize whatever is the first character of the string, but what if I have an acronym for, let's say, chemical names like 3-azido-1-propylamine and I want to use \Acl to print 3-Azido-1-propylamine. Would it be possible to capitalize the first letter? – Sam Oct 27 '19 at 11:24
  • @Sam it's probably beyond me. I've never had to use such acronyms, and would for style reasons always avoid starting a sentence with a numeral, partly because I wouldn't know whether to capitalise the letter. But I can see that you'd want to start sentences with chemical names in a chemistry paper. If I were you I'd look at whether the glossaries package can do this. Its much more advanced and the author is active here – Chris H Oct 27 '19 at 17:39
  • ... However egreg, on whose answer I based this, is a LaTeX genius and probably could – Chris H Oct 27 '19 at 17:40
4

An easier approach is simply writing the desired output of the acronym in full, not using any of the acronym commands to output it, and follow this by \acused{TLA}.

\acused Marks an acronym as used, as if it had been called with \ac, but without printing anything. This means that in the future only the short form of the acronym will be printed.

Although a drawback here is the occurrence of the acronym at this position won't be updated based on earlier/later occurances, other occurances will still be updated based on the current occurance. For shorter texts the overhead of having to track these 'custom' occurances manually might not be severe.

2

An alternative approach if you're happy to switch packages:

\documentclass{article}
\usepackage[shortcuts,acronyms]{glossaries-extra}
\newacronym{TLA}{TLA}{three-letter acronym}
\begin{document}
...end of one sentence. \Aclp{TLA} are acronyms with three
letters...

\printunsrtglossary[type=\acronymtype]
\end{document}

Produces:

...end of one sentence. Three-letter acronyms are acronyms with three letters...

Acronyms

TLA three-letter acronym

Nicola Talbot
  • 41,153