4

I've recently encountered a problem when trying to combine

\usepackage[french]{babel}

and

\graphicspath{{c:/Users/...}}

A problem then appears when I try to use \includegraphics (although it perfectly works when french is not selected in babel.) It appears that babel (with french preset) modify the behavior of the colon (as a spacing is required before it in French). This problem has already been mentioned here and some people propose to avoid the use of the colon. In case of a windows Path, its not possible.

For the moment, I've found a simple hack :

\usepackage[english,main=french]{babel}

and turn to English before including graphics and then return to French. This solution works utbut is not easy to use as I'm maintaining a repository of various tex files (>1000) used by various users and I can't make them use this trick each time.

Any idea about a better solution ?

  • 4
    if you use \graphicspath in the preamble it should not be a problem, the colon is not active there yet. But as you didn't show a complete example, it is difficult to say what you are actually doing. – Ulrike Fischer Dec 22 '20 at 14:08
  • Ok, I didn't know. I did not put a MWE as I also encountered the problem with the import macro, which cannot be used in the preamble. – M. Miguel-Brebion Dec 22 '20 at 14:13
  • This question is clear enough and shouldn't have been closed. Ulrike's @UlrikeFischer comment could be made into an answer. – Donald Arseneau Jan 03 '21 at 23:40

2 Answers2

2

You can load babel with the shorthands= option, for example,

\documentclass[french]{article}
\usepackage[main=french, shorthands=off]{babel}

You can instead whitelist specific shorthands, instead of turning them off, if you need some other shorthands. The syntax for this is in the babel manual.

You could also turn off shorthands temporarily, without switching languages, with \languageshorthands{none}. When you need them again, you can restore \languageshorthands{french}. If you need to wrap an \originalmacro, you might try something like:

\newcommand\mymacro[1]{%
  \languageshorthands{none}%
  \originalmacro{#1}%
  \languageshorthands{\languagename}}

You could alternatively set the \catcode of : and other characters that are breaking your commands to 12 within a \begingroup, to make them inactive:

\newcommand\mymacro[1]{%
  \begingroup%
    \catcode`:=12%
    \originalmacro{#1}%
  \endgroup}

Finally, some packages have their own options to disable shortands inside commands they need to parse, such as the babel library of TikZ, which makes the TikZ parser Babel-shorthand-compatible.

Davislor
  • 44,045
  • This sound simpler than switching from one to another language but still need to be done before and after each call to macro which use windows paths. – M. Miguel-Brebion Dec 22 '20 at 17:03
  • @M.Miguel-Brebion You could write your own wrapper for those macros. Does \languageshorthands{none}\originalmacro\languageshorthands{\languagename} work? – Davislor Dec 23 '20 at 00:03
  • @M.Miguel-Brebion Posted some examples. (Warning: untested.) – Davislor Dec 23 '20 at 00:10
  • At least your second example will not work because the colon will already be tokenized with a fixed catcode. I'm fairly sure that the first won't work for the same reason, too. – cgnieder Jan 03 '21 at 21:48
2

The root problem is that there are contexts in LaTeX that are essentially "moving" which the \protection mechanism does not handle. These include file names.

Rather than disabling language shorthands (if, indeed, you do want shorthands), I suggest manually applying the proper protection:

\graphicspath{{C\string:/Users/myself/Pictures}}

(Don't use the standard \protect but \string instead.)