[Tex/LaTex] Automatic german quotation marks

germanpunctuation

I would like to compile a file according to German standards, in which quotation marks are to be put like this:
\glqq text \grqq

In my LaTeX document I use quotation marks in the standard way: ''text''

Is there a possibility to change in the preamble that '' will be compiled – depending on its location to \glqq or \grqq? Or an even simpler solution?

EDIT: Sorry for the incomplete desciption. I was referring to the german standards which are: „“

Best Answer

Probably this will cause some other troubles, but here it is.

Elaborating a bit on this answer of Martin Scharrer, this is the result:

enter image description here

Code:

\documentclass{article}
\usepackage[ngerman]{babel}

\let\oldquote'
\newif\ifquoteopen
\catcode`\'=\active
\makeatletter
\DeclareRobustCommand*{'}{%
   \@ifnextchar'{%
     \ifquoteopen
       \global\quoteopenfalse\grqq\expandafter\@gobble
     \else
       \global\quoteopentrue\glqq\expandafter\@gobble
     \fi
   }{\oldquote}%
}
\makeatother

\begin{document}
\section{Using ''quotes''}
A ''quote'' and one with a period that follows: ''quote''. And a single 'quote'.

\noindent
And this is the original one: \glqq quote\grqq.
\end{document} 

If the meaning of ' in math mode is to be preserved, some other hacks are needed:

\documentclass{article}
\usepackage[ngerman]{babel}

\let\oldquote'
\newif\ifquoteopen
\catcode`\'=\active
\makeatletter
% we have to redefine \pr@m@s to use an active '
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}
\protected\def'{%
  \ifmmode
    \expandafter\active@math@prime
  \else
    \expandafter\active@text@prime
  \fi}
\def\active@text@prime{%
   \@ifnextchar'{%
     \ifquoteopen
       \global\quoteopenfalse\grqq\expandafter\@gobble
     \else
       \global\quoteopentrue\glqq\expandafter\@gobble
     \fi
   }{\oldquote}%
}
\makeatother

\begin{document}
\section{Using ''quotes''}
A ''quote'' and one with a period that follows: ''quote''. And a single 'quote'.

\noindent
And this is the original one: \glqq quote\grqq.

\noindent
Some derivatives $f'(x)+g''(x)$.
\end{document} 

enter image description here


This last solution also transforms single quotes into German ones:

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{xspace}

\let\oldquote'
\newif\ifquoteopen
\catcode`\'=\active
\makeatletter
% we have to redefine \pr@m@s to use an active '
\def\pr@m@s{%
  \ifx'\@let@token
    \expandafter\pr@@@s
  \else
    \ifx^\@let@token
      \expandafter\expandafter\expandafter\pr@@@t
    \else
      \egroup
    \fi
  \fi}
\protected\def'{%
  \ifmmode
    \expandafter\active@math@prime
  \else
    \expandafter\active@text@prime
  \fi}
\def\active@text@prime{%
   \@ifnextchar'{%
     \ifquoteopen
       \global\quoteopenfalse\grqq\expandafter\@gobble
     \else
       \global\quoteopentrue\glqq\expandafter\@gobble
     \fi
   }{%
     \ifquoteopen
       \global\quoteopenfalse\grq\xspace
     \else
       \global\quoteopentrue\glq
     \fi
   }%
}
\makeatother

\begin{document}
\section{Using ''quotes''}
A ''quote'' and one with a period that follows: ''quote''. A single 'quote' and one with a period that follows: 'quote'.

\noindent
And these are the original ones: \glqq quote\grqq{} and \glq quote\grq.

\noindent
Some derivatives $f'(x)+g''(x)$.
\end{document} 

enter image description here

Related Question