4

I have almost the same issue as the OP in this question: I use the natbib package with specific parameters set, in my case as in this small example:

\documentclass{article}
\usepackage[                                % Citations formatting
  numbers,    %% Numbered instead of author+year
  square,     %% In square brackets
  comma,      %% Comma-seperated
  compress,   %% Range instead of sequence
  super       %% Superscript
]{natbib}

\begin{document}


Cite works perfectly\cite{ref1} but making it non-superscript does not [\citenum{ref1}].


\begin{thebibliography}{1}
\bibitem{ref1} Author, Title, Publisher, Location, Edition, Year.
\end{thebibliography}

\end{document}

These package settings give me numbered references in square brackets and in superscript (raised above the line) when using \cite{...}. As wanted.

I do though occasionally wish to remove superscript and just have an inline reference written out as [1].

Apparently, this is not easy. There seems to be no way to remove super from specific occurrences of \cite{...}. Instead the documentation gives the command \citenum{...} which removes the superscript and displays only the pure reference number 1 - but also the square brackets are removed!

I then thought "no problem, I'll just manually add the square brackets and write [\citenum{...}]" as shown in the working example above. But this gives this result:

enter image description here

There is an extra space in [ 1]!

So, the question is: How do I go around this and get a non-superscript reference in square brackets without any spaces added?

Edit

The linked question offers a solution by defining a new custom parameter. For my purpose thise seems like much overkill, since I need the non-superscript reference written in one single tiny place. I was asking to find out if a simpler method was available, which I might have missed, for example if whitespace could be stripped by another latex-command or if the package could be tweeked easily. I guess that is not the case based on the lack of responses.

10
  • Why don't you use \citenst from locksteps answer to the question you linked? Works like a charm
    – DG'
    May 23, 2017 at 8:15
  • 1
    try more braces... :) seriously, I would
    – doed
    Jun 2, 2017 at 13:46
  • @doed I'm sorry, I don't understand what you mean?
    – Steeven
    Jun 2, 2017 at 14:18
  • @DG' I need this feature for a very simple thing. Seems like much overkill to define a new parameter for that tiny purpose. I was asking to find out if a simpler method was available, which I might have missed, if whitespace could be stripped by another latex-command or if the package could be tweeked easily. I guess that is not the case.
    – Steeven
    Jun 26, 2017 at 13:57
  • 2
    A very hacky way would be to add negative spacing like [\kern-.4em\citenum{...}], for example? I would only do this if your use of this is limited.
    – Troy
    Jun 26, 2017 at 14:17

3 Answers 3

5
+50

You just need to tell natbib that you want the brackets. By default, \citenum sets \NAT@parfalse (no brackets).

\begin{filecontents*}{\jobname.bib}
@article{test1,
  author={A. Uthor},
  title={Title},
  journal={Journal},
  year=2017,
}
@article{test2,
  author={W. Riter},
  title={Title},
  journal={Journal},
  year=2016,
}
\end{filecontents*}

\documentclass{article}
\usepackage[  % Citations formatting
  numbers,    %% Numbered instead of author+year
  square,     %% In square brackets
  comma,      %% Comma-seperated
  compress,   %% Range instead of sequence
  super       %% Superscript
]{natbib}

\usepackage{xpatch}
\makeatletter
\xpatchcmd\citenum{\NAT@parfalse}{\NAT@partrue}{}{}
\makeatother

\begin{document}

With \citenum{test1} blbla\cite{test1}

With \citenum{test1,test2} blbla\cite{test1,test2}

\bibliographystyle{plainnat}
\bibliography{\jobname}

\end{document}

enter image description here

With the sort&compress option instead of compress, you'd get

enter image description here

2
  • Thank you very much for the answer. May I ask why the \makeatletter and \makeatother commands are necessary before and after the patch, respectively?
    – Steeven
    Jul 9, 2017 at 15:54
  • 1
    @Steeven Commands with @ in their name (internal commands) are involved. See What do \makeatletter and \makeatother do?
    – egreg
    Jul 9, 2017 at 16:04
4

For LaTeX standards, the solution by @lockstep is (sadly) not so much overkill, and I would advise you to use it. However, for a one-off it can be done a bit easier.

You can locally redefine citenumfont to include the brackets, which does not add a space. Note that you need to do this in a group, otherwise regular citations get two sets of brackets.

You can also apply the easy but risky manual negative space workaround (cf. the comment by @Troy).

MWE:

\documentclass{article}
\usepackage[                                % Citations formatting
  numbers,    %% Numbered instead of author+year
  square,     %% In square brackets
  comma,      %% Comma-seperated
  compress,   %% Range instead of sequence
  super       %% Superscript
]{natbib}

\begin{document}
With \texttt{citenumfont}: {\renewcommand{\citenumfont}[1]{[#1]}\citenum{abc}}

With negative \texttt{hspace}: [\hspace*{-4px}\citenum{abc}]

Regular citation\cite{abc}

\bibliographystyle{plain}
\bibliography{sampleref}

\end{document}

Result:

enter image description here

4

The white space is inserted throught the command \NAT@spacechar. You could redefine this command to do nothing, but as it is used in other places too, this could have the unwanted side-effect to remove spaces where you don't want it.

A safer solution is too patch \citenum:

\documentclass{article}
\usepackage[                                % Citations formatting
  numbers,    %% Numbered instead of author+year
  square,     %% In square brackets
  comma,      %% Comma-seperated
  compress,   %% Range instead of sequence
  super       %% Superscript
]{natbib}

%\makeatletter
%\let\NAT@spacechar\relax
%\makeatletter

\usepackage{xpatch}
\makeatletter
\xpatchcmd\citenum{\NAT@spacechar}{\unkern}{}{\fail}
\makeatother

\begin{document}
With [\citenum{ctan}] blbla


\bibliographystyle{plain}
\bibliography{biblatex-examples}

\end{document}

enter image description here

4
  • 2
    I tried something similar earlier today. Maybe it's just me, but the spacing looks wrong: The 1 doesn't look centered in the brackets.
    – TH.
    Jun 27, 2017 at 7:54
  • @TH. You are right there is a (hard coded) \kern of 1pt in the code. So one shouldn't use \relax, but a command that cancels this kern. Jun 27, 2017 at 8:31
  • \unkern seems to work.
    – TH.
    Jun 27, 2017 at 8:40
  • @TH. Ah yes, thanks. I somehow thought that there is something in between. Corrected. Jun 27, 2017 at 8:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .