\newgeometry doesn't work with Turkish babel package

\newgeometry is a nice feature of geometry package. I found out that it doesn't work (together with many other problems) when \usepackage[turkish]{babel} is also used with many errors stating:

Missing \endcsname inserted...

This seems to be a bug in Turkish babel which inserts space next to every "=" sign. I found a workaround:

\shorthandoff{=}
\newgeometry{top=1cm}
\shorthandon{=}


Any better ideas on getting this to work?

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Feb 14 '14 at 8:31
• Same problem as here: tex.stackexchange.com/q/32178/27635. It seems that the problem is still unsolved. – karlkoeller Feb 14 '14 at 8:49
• @karlkoeller exactly. Is there an automatic way to do this, i am now having problems with every \includegraphics too. I mean automatically issue \shorthandoff{=} before \includegraphics then \shorthandon{=} after it. I have been searching around with no answers up to now. Should I ask this in a separate question? – Sinan Feb 14 '14 at 9:45

A solution for \includegraphics as you've requested in the above comments.

Add the following lines in the preamble:

\usepackage{etoolbox}
\let\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
\oldincludegraphics[#1]{#2}%
\shorthandon{=}%
}
\pretocmd{\includegraphics}{\shorthandoff{=}}{}{}


We first redefine \includegraphics to add \shorthandon{=} after it and then, through the \pretocmd command from the etoolbox package, we prefix \includegraphics with \shorthandoff{=}.

Complete MWE

\documentclass[]{article}
\usepackage[turkish]{babel}
\usepackage{graphicx}

\usepackage{etoolbox}
\let\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
\oldincludegraphics[#1]{#2}%
\shorthandon{=}%
}
\pretocmd{\includegraphics}{\shorthandoff{=}}{}{}

\begin{document}

\includegraphics[width=4cm]{example-image-a}

\end{document}


Output:

• Is there a reason that you don’t add \shorthandoff directly in the redefinition? – Tobi Feb 14 '14 at 11:05
• @Tobi, because it doesn't work. – karlkoeller Feb 14 '14 at 11:06
• Any idea why this doesn’t work? – Tobi Feb 14 '14 at 11:47
• Thanks, and I must say I am mystified about things that mystically don't work in LaTeX – Sinan Feb 14 '14 at 12:02
• @Tobi: Catcodes cannot be changed after reading and tokenizing. If the definition text of \includegraphics is executed, then it has already seen and tokenized its arguments. Changing the catcodes with \shorthandoff{=} comes too late. – Heiko Oberdiek Feb 14 '14 at 15:20

\usepackage[turkish]{babel} makes = active. That disturbs key value parser, because they usually assume that the syntax characters = and , have the usual catcode (other=12). The official way is indeed using

\shorthandoff{=}
% stuff that does not expect an active =', e.g.
\newgeometry{top=1cm}
\shorthandon{=}


Package keyval: \kvsetkeys

A more comfortable way is provided by package kvsetkeys. Its key value parser was written with turkish.ldf in mind. Thus it supports active syntax characters in key value lists. The package defines \kvsetkeys, which can now be used as direct replacement for \setkeys of package keyval:

\usepackage{keyval,kvsetkeys}
\let\setkeys\kvsetkeys


Package xkeyval

The package directly redefines and extends \setkeys:

\usepackage{xkeyval}


hen active equal signs are supported in key value lists that are processed by \setkeys.

Compatibility

Package xkeyval also changes more things, like the parsing of package and class options, even for packages that do not use or know xkeyval. Therefore I consider the solution with kvsetkeys less intrusive.

However, both solutions can easily cooperate, even if xkeyval might be loaded by the class or other packages. The trick is using the first method as early as possible:

\RequirePackage{keyval,kvsetkeys}
\let\setkeys\kvsetkeys

% classes and packages that might load xkeyval
\documentclass{...}

% packages in any order, e.g.
\usepackage{geometry}
\usepackage[turkish]{babel}
\usepackage{graphicx}

\begin{document}

% then the following works without \shorthandoff{=}:
\newgeometry{top=1cm}
\includegraphics[width=\linewidth]{...}


Then active equal signs are supported by package kvsetkeys as default or by xkeyval for the case that it is loaded later.

• But the following don't work this time: \tikz[remember picture,overlay] {% \draw [thick,red!50!black] ([shift={(+10mm,-10mm)}]current page.north west) rectangle ([shift={(-10mm,+10mm)}]current page.south east) ; }% – Sinan Feb 14 '14 at 12:20
• @Sinan: tikz has its own key value parser, package pgfkeys and contains syntax stuff that include the equals sign outside of key value pairs. Thus "fixing" \setkeys will not help and you need \shorthandoff{=}. – Heiko Oberdiek Feb 14 '14 at 15:17

With babel 3.9 (if you are not using it, upgrade, because it fixes tons of bugs):

\usepackage[turkish,shorthands=off]{babel}


or if you want : and !:

\usepackage[turkish,shorthands=:!]{babel}


The Turkish style has been upgrade recently, too. The new manual explains how to deal with the =.

Don't use \shorthandon{=}. With following code, it seems there is no problem.

\documentclass[demo]{article}
\usepackage[turkish]{babel}
\usepackage[utf8]{inputenc}
\usepackage[top=2cm]{geometry}
\usepackage{graphicx}
\usepackage{lipsum}
\begin{document}
\shorthandoff{=}
\newgeometry{top=1cm}
\section{ğüşiöç}
\lipsum[1]
\includegraphics[scale=1]{}
\newgeometry{top=10cm}
\section{ĞÜŞİÖÇ}
\lipsum[2]
\includegraphics[scale=1]{}
\end{document}
`