2

I'd like to remove hyphenation at the end of a line on words like 'section', 'Table', 'Figure' etc. when using the \cref command with the Cleveref package. My current solution is the following:

\crefname{table}{\mbox{Table}}{Tables}

But I'm not sure if this is the best way to do it, since I'd have to repeat this command for figures, equations, etc.

I tried the following but believe that there is an infinite recursion issue:

\renewcommand{\cref}[1]{\cref{#1}}

So tried this, but to the same issue:

\newcommand{\crefboxed}[1]{\mbox{\cref{#1}}}
\renewcommand{\cref}[1]{\crefboxed{#1}}

Any insight?

0

2 Answers 2

2

Answering ti the infinite recursion problem:

\let\oldcref\cref

and after that

\renewcommand{\cref}[1]{\mbox{\oldcref{#1}}}

or

\def\cref#1{\mbox{\oldcref{#1}}}

About the problem of more arguments that Mico mentioned you could do this:

\def\cref#1{\foreach\arg[count=\j from 1] in {#1}{\xdef\NumArgs{\j}}\foreach \argument[count=\i from 1] in {#1}{\ifnum\i=1\mbox{\oldcref{\argument}}\else \ifnum\i=\NumArgs{} and \ref{\argument}\else, \ref{\argument}\fi\fi}}

Could work only for same type of arguments (only tables or only figures etc)... Anyway my answer was about the infinite recursion... (But thanks for the comment @Mico)

PS: You need pgffor package for this..

8
  • Thank you for that insight. Is there a benefit to using \renewcommand vs. \def? Commented Aug 27, 2019 at 20:28
  • Umm, this method has a serious downside if the argument of \cref contains two or more arguments. In that cases, this method will not only suppress hyphenation in the word figure (or table, or whatever), but also prevent any and all line breaks elsewhere in the cross-references. E.g., if \cref{fig:2,fig3} is supposed to output the string Figures 2 and 3, the presence of \mbox will also disallow a line break between the substrings Figures 2 and and 3. That's not what the OP specified -- or might conceivably want, really.
    – Mico
    Commented Aug 27, 2019 at 20:47
  • @likethevegetable it generally adviced to use \renewcommand in LaTeX ... \def is a TeX command that for example does not informs you that the command is already defined ... But I use \def often (that's my problem I suppose). Something not about this example: Just keep in mind, that \def can work if \cref is or is not defined... but you have to be careful with \newcommand if already defined or \renewcommand if not defined.
    – koleygr
    Commented Aug 27, 2019 at 20:47
  • @koleygr - Many thanks for addressing the issue I raised. I'm afraid I can't get your code to run as is (and, yes, it's not because I failed to load pgffor).
    – Mico
    Commented Aug 27, 2019 at 21:10
  • 1
    (but prints table instead of tables ...)
    – koleygr
    Commented Aug 27, 2019 at 21:34
5

If you wish to suppress hyphenation of the words section, table, and figure when using \cref, you will probably also want to suppress hyphenation of these words in other circumstances. If that's the case, simply run

\hyphenation{section table figure}

in the preamble.

If you do wish to limit hyphenation suppression of figure, table, and section to what's output by \cref, just add the following instructions to the preamble (after loading cleveref, naturally:

\crefname{section}{\mbox{section}}{\mbox{sections}}
\Crefname{figure}{\mbox{Figure}}{\mbox{Figures}}
\crefname{table}{\mbox{table}}{\mbox{tables}}

If you happen to run \Cref as well as \cref, you'll also need to add

\Crefname{section}{\mbox{Section}}{\mbox{Sections}}
\Crefname{figure}{\mbox{Figure}}{\mbox{Figures}}
\Crefname{table}{\mbox{Table}}{\mbox{Tables}}
1
  • Wow, I can't believe I didn't know of the \hyphenation{...} command.. thank you very much, this solves the problem and more. Commented Aug 27, 2019 at 20:25

You must log in to answer this question.

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