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.

Steeven
  • 1,407
  • Why don't you use \citenst from locksteps answer to the question you linked? Works like a charm – DG' May 23 '17 at 08:15
  • 1
    try more braces... :) seriously, I would – doed Jun 02 '17 at 13:46
  • @doed I'm sorry, I don't understand what you mean? – Steeven Jun 02 '17 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 '17 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 '17 at 14:17
  • I'm not sure what you mean by defining a new parameter. Do you mean you don't want to define a new macro? You don't have to. You could just take the replacement text of lockstep's macro and write it where you need it. But just using the macro seems easiest. – TH. Jun 26 '17 at 14:36
  • @Steeven As TH pointed out already, lockstep's macro or command if you will (although not parameter) is the easy way. Just copy the code from makeatletter to \makeatother and paste it under `\usepackage[...]{natbib} and you're good to go. Alternatively save the code in a file and include it. – DG' Jun 26 '17 at 14:59
  • @Steeven Adding a MWE to you question would be much more helpful than offering a bounty. – samcarter_is_at_topanswers.xyz Jun 27 '17 at 08:32
  • @samcarter Done. – Steeven Jun 27 '17 at 08:52

3 Answers3

5

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

egreg
  • 1,121,712
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

Marijn
  • 37,699
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

Ulrike Fischer
  • 327,261