1

I'm trying to use swedish date format by when using the package datetime2. I have found the following link https://www.ctan.org/pkg/datetime2-swedish but I don't understand how to call for this one. Could anyone help me? This is what I am trying to do:

\documentclass[paper=a4,fontsize=11pt]{article} 
\usepackage{datetime2}

\begin{document}   
\begin{minipage}{1\textwidth}
\begin{flushright}
\Large{ \textbf{ Joe Biden }}
\noindent

\hfill \today
\end{flushright}
\end{minipage}
\end{document}

I want this to write e.g. 4e november 2016

As can be seen in the answers and comments this was a problem that was due to a temp.cls - file that was used in a template that I used. Although it has answers on questions when not using this as well.

Thanks!

  • You need to give us more information, among others a full minimal example. I;ve never heard of a document class named temp – daleif Nov 4 '16 at 10:45
  • I see. I got this from a template when making a resumé.. I'll try to edit the question to create a minimal example, although I am not entirely sure how to do that. :) Just wait a second. – MIKAEL B Nov 4 '16 at 10:52
  • Could the temp document class come from that it is KOMA-article class? If so, how do you do to use KOMA in a new document? I'm using overleaf – MIKAEL B Nov 4 '16 at 13:46
  • For now in our tests just use the normal article class instead of temp. It is a lot easier to make examples fro others when you use a setup that others can understand. Also datetime2-swedish is not a latex package, it is a configuration for datetime2, it is used when datetime2 detects that it needs to use swedish lanuage, for example like in my MWE – daleif Nov 4 '16 at 13:54
  • Ah, ok.. I have found the solution: the "temp" comes from a .cls file that is added to the template that I used. And when I'm trying to use the "article"-class other commands stops working, such as the one called "\MyName". However, when I'm using the temp-class the other answer you made does not work. Does this mean I have to chose one or another? So you mean the swedsih is "baked in to" the datetime2-package when I use it? If so, how do I "call" it? – MIKAEL B Nov 4 '16 at 14:05
7

The comments on @daleif's answer have become a little convoluted, so it's probably easier if I try to summarise it here as an extension to the other answer.

LaTeX's default format of \today is the US style in the form "November 4, 2016". For example:

\documentclass{article}

\begin{document}
\today
\end{document}

The babel package loaded with the option swedish redefines \today to use the Swedish form "4 november 2016":

\documentclass{article}

\usepackage[swedish]{babel}

\begin{document}
\today
\end{document}

However, you can't customise this if you want to make some slight modifications (such as have "4e" instead of just "4") and there's also no provision for formatting a specific date. The datetime2 package provides this extra functionality, but it needs to know what language you're using in your document. Luckily, datetime2 loads the tracklang package which can find out whether babel or polyglossia or whatever has been used, and it can tell datetime2 what language(s) you've already requested.

So you can just do:

\documentclass{article}

\usepackage[swedish]{babel}
\usepackage[useregional=text]{datetime2}

\begin{document}
\today
\end{document}

Which produces "4 november 2016" (or you can useregional=numeric to get a numeric date in your regional format). Thanks to the tracklang package, datetime2 knows that it needs to load datetime2-swedish.ldf, which provides the Swedish date styles (swedish and swedish-numeric). It's this datetime2-swedish.ldf file that provides the command \DTMswedishordinal which formats the ordinal part of the swedish textual date style.

This can be redefined. For example:

\documentclass{article}

\usepackage[swedish]{babel}
\usepackage[useregional=text]{datetime2}

\renewcommand*{\DTMswedishordinal}[1]{\number#1e}

\begin{document}
\today
\end{document}

This produces "4e november 2016".

Other variations include specifying the language as a document class option:

\documentclass[swedish]{article}

\usepackage{babel}
\usepackage[useregional=text]{datetime2}

\renewcommand*{\DTMswedishordinal}[1]{\number#1e}

\begin{document}

\today

\end{document}

In fact, datetime2 can be used without babel. For example

\documentclass{article}

\usepackage[swedish]{datetime2}

\renewcommand*{\DTMswedishordinal}[1]{\number#1e}

\begin{document}

\today

\end{document}

Things that can go wrong:

  • The package loading is in the wrong order. If babel is loaded after datetime2 instead of before, it's too late for datetime2 to override babel's date hook. It's also too late to pick up the required language unless it's been passed as a document class option or directly to datetime2.
  • The class or another package might try redefining \today at the start of the document. For example, the following mimics a class that does this:

    \AtBeginDocument{\def\today{\ifcase\month\or January\or February\or March\or
    April\or May\or June\or July\or August\or September\or October\or
    November\or December\fi \space \number \day, \number \year}}
    
    \documentclass{article}
    
    \usepackage[swedish]{babel}
    \usepackage[useregional=text]{datetime2}
    
    \renewcommand*{\DTMswedishordinal}[1]{\number#1e}
    
    \begin{document}
    \today
    \end{document}
    

    This now produces "November 4, 2016" because of the redefinition in \AtBeginDocument.

There's also the possibility that another package loaded after datetime2 does something similar. To provide a way to counteract this, datetime2 provides \DTMtoday (new to version 1.4) which is datetime2's version of \today. You can use it directly or redefine \today at the beginning of the document:

\AtBeginDocument{\def\today{\ifcase\month\or January\or February\or March\or
April\or May\or June\or July\or August\or September\or October\or
November\or December\fi \space \number \day, \number \year}}

\documentclass{article}

\usepackage[swedish]{babel}
\usepackage[useregional=text]{datetime2}

\renewcommand*{\DTMswedishordinal}[1]{\number#1e}

\begin{document}
\DTMtoday
\end{document}

This produces "4e november 2016", even though \today is back to the original US style. Alternatively:

\AtBeginDocument{\def\today{\ifcase\month\or January\or February\or March\or
April\or May\or June\or July\or August\or September\or October\or
November\or December\fi \space \number \day, \number \year}}

\documentclass{article}

\usepackage[swedish]{babel}
\usepackage[useregional=text]{datetime2}

\renewcommand*{\DTMswedishordinal}[1]{\number#1e}

\begin{document}
\let\today\DTMtoday

\today
\end{document}

If you have an old version of datetime2 that doesn't recognise \DTMtoday, then you can save and restore datetime2's version of \today like this:

\AtBeginDocument{\def\today{\ifcase\month\or January\or February\or March\or
April\or May\or June\or July\or August\or September\or October\or
November\or December\fi \space \number \day, \number \year}}

\documentclass{article}

\usepackage[swedish]{babel}
\usepackage[useregional=text]{datetime2}
\let\dtmtoday\today

\renewcommand*{\DTMswedishordinal}[1]{\number#1e}

\begin{document}
\let\today\dtmtoday

\today
\end{document}

It turns out the problem is because your class file temp.cls is loading babel with english. This means that the main document language is English not Swedish so you effectively have a situation analogous to:

\documentclass[swedish]{article}
\usepackage[english]{babel}

\begin{document}
\today
\end{document}

This sets the main language to english with swedish as an auxiliary language. You can override this by inserting the following line before the document class is loaded:

\PassOptionsToPackage{main=swedish}{babel}

This will ensure that babel recognises that swedish is the main document language.

  • Wow, this is such an extensive and impressive answer that it must be the right one. Although it still doesn't work. It must be something with the temp-file that does it. I don't know if this is allowed, but here is a link for the project on overleaf: overleaf.com/6883177jyrmgbgwbcqn#/23496337 THANK YOU for the amazing answer! – MIKAEL B Nov 7 '16 at 11:17
  • @MIKAELB Your temp class loads babel with english, which is why the date is using the US format. If you change english to swedish in your temp.cls file that should fix the problem. – Nicola Talbot Nov 7 '16 at 11:24
  • Thank you! I see. How do you do to make changes in teh temp.cls-file? – MIKAEL B Nov 7 '16 at 11:59
  • @MIKAELB It looks like the class file can't be edited. I've modified my answer. – Nicola Talbot Nov 7 '16 at 15:51
  • Wow, it works now. I had sort of given up the hope of this. THANK YOU! Does this last command (\PassOptionsToPackage) force swedish to be the main language independent of what you say after? I.e does it overrule all other language-commands? Thanks again! – MIKAEL B Nov 8 '16 at 12:29
2

Do you really write it as 4e november in Sweden, in DK we use 4. november

Here is an example that works, but does not give you quite the correct output.

 \documentclass[a4paper,swedish]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage[useregional=text]{datetime2}

\begin{document}   
 \today

\end{document}
  • Hmm, you could write: 4 november 2016 (no period) Although I do not get this one to work either... Not with my document – MIKAEL B Nov 4 '16 at 11:41
  • @MIKAELB you'll probably need to do your own definition then. datetime2 should allow you to do that (see the manual), most of Nicolas stuff are highly configurable. – daleif Nov 4 '16 at 12:03
  • I see. Could you explain how you do that? I'm fairly new to the latex concept.. – MIKAEL B Nov 4 '16 at 13:25
  • @MIKAELB nope, I don't use the package (and have other things to do), but you should be able to work your way through it via the manual and by looking up datetime questions here (you can usually just googl "latex datetime" and you'll get questions from here. – daleif Nov 4 '16 at 13:44
  • 1
    @MIKAELB The datetime2-swedish.ldf file is loaded implicitly by datetime2 if it detects the swedish language option. The swedish option can be in the document class with \usepackage[useregional]{datetime2} (as in @daleif's MWE) but only if the document class uses the standard \DeclareOption method of defining options. Since your temp class options seem to be using a key=value interface you may have to use \usepackage[swedish]{datetime2} (or \usepackage[swedish]{babel}\usepackage[useregional]{datetime2} if you're using babel) instead. – Nicola Talbot Nov 4 '16 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.