[Tex/LaTex] \newgeometry doesn’t work with Turkish babel package

babelgeometrymargins

\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?

Best Answer

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:

enter image description here