68

[french]{babel} (aka frenchb) is incompatible with some other packages, such as tikz, since it tries to insert \thinspace before semi-colons.

In order to make these two play nice, you have to call \shorthandoff{;} in the document.

Now I have a package that uses tikz and I would like to locally disable the semi-colon for babel, instead of disabling it for the whole document. I could hack the .sty of the package to do that, knowing that I'm using frenchb, but I would rather make something clean and portable.

Is there a way in my package that I can test if frenchb is loaded and disable semi-colon management with babel locally, so the package doesn't have to be hacked?

Edit:

Martin's solution gives me something like this in general:

\RequirePackage{ifthen}
\newcommand{\@babel@loaded}{0}
\AtBeginDocument{\@ifpackageloaded{babel}                                                 
  {\renewcommand{\@babel@loaded}{1}}                                                      
  {\renewcommand{\@babel@loaded}{0}}                                                      
}
\newcommand{\somecommandusingsemicolon}{
  \ifthenelse{\@babel@loaded=1}{\shorthandoff{;}}{}  
  % code with semi-colon here
}

and in the specific case of tikz:

\AtBeginDocument{\@ifpackageloaded{babel}
  {%
    \g@addto@macro\tikz@installcommands{%
      \shorthandoff{;}%
    }
    \g@addto@macro\tikz@uninstallcommands{%
      \shorthandon{;}%
    }
  }
  {\relax}
}
5

1 Answer 1

86

You can use the internal LaTeX macro \@ifpackageloaded to test if a package was loaded:

\@ifpackageloaded{<name>}{<true>}{<false>}

There are also:

\@ifpackagewith{<name>}{<option-list>}{<true>}{<false>}
\@ifpackagelater{<name>}{<date YYYY/MM/DD>}{<true>}{<false>}

For further macro see the document linked in Documentation reference for LaTeX internal commands?.


In the case of tikz it would be best to add your code to the macros \tikz@installcommands and \tikz@uninstallcommands. The first is executed at the begin of a tikzpicture and enables all the tikz commands only defined there and the latter is executed at the begin of node content and changes the definitions back to there normal state. This way you can have French text inside the nodes.

You can add code to the macros using:

\AtBeginDocument{\@ifpackageloaded{babel}{%
    \g@addto@macro\tikz@installcommands{%
      \shorthandoff{;}%
    }%
    \g@addto@macro\tikz@uninstallcommands{%
      \shorthandon{;}%
    }%
}{}

I'm just guessing here that \shorthandon is the opposite of \shorthandoff.

5
  • @Martin: \@ifclassloaded seems to be only usable in preamble. Is that right?
    – raphink
    Apr 19, 2011 at 21:20
  • 2
    @Raphink: Yes, they are marked as \@onlypreamble. You would need to test for the package in question at the end of the preamble, e.g. using \AtBeginDocument{...}, and set a global flag for your document macros. Apr 19, 2011 at 21:24
  • @Martin: Right. I would like to disable semi-colon management very locally, in a given macro. If I do a \AtBeginDocument, it will still affect the whole scope.
    – raphink
    Apr 19, 2011 at 21:25
  • 3
    @Raphink: Right, that's why I wrote "set a global flag for your document macros" not "change your settings". You then use that flag locally. Apr 19, 2011 at 21:30
  • @Martin: I edited the question to make an example with your proposal. Is that correct?
    – raphink
    Apr 19, 2011 at 21:33

You must log in to answer this question.

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