3

I use the babel package in order to write in greek with font "kerkis".

I want to add code in english ("courier new" font) to my latex document but I get some difficulties.

Can anybody help me, please. Thank you in advance.

\documentclass[12pt]{article}
\usepackage[english,greek]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{graphicx}
\usepackage{fancybox}
\usepackage{verbatim}
\usepackage{courier}
\usepackage{kerkis}
\usepackage{float}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{tocloft}
\usepackage{hyperref}
\usepackage{afterpage}
\usepackage{color}   
\usepackage{listings}

\renewcommand{\lstlistingname}{Κώδικας}

\lstset{ 
%matlab
  backgroundcolor=\color{white},  
  basicstyle=\normalsize,    % COURIER NEW?     
  commentstyle=\color{green},    % comment style
  escapeinside={\%*}{*)},        
  extendedchars=true,            
  frame=single,                           
  keywordstyle=\color{blue},       % keyword style
  numbers=left,                  
  numbersep=5pt,                  
  numberstyle=\normalsize\color{black}, 
  rulecolor=\color{black},        
  stepnumber=1,                   
  stringstyle=\color{magenta},     % string style
} 

\begin{document}

\selectlanguage{greek}
Ελληνικά: \textlatin{this word means greek}

\selectlanguage{english}
Now I write in english

\selectlanguage{greek}
Ελληνικά

%\selectlanguage{english}???

\begin{lstlisting}[caption = \textlatin{example1.m}]
{
% Entropy
E = 0;
for i = 1:J
    if p(i) ~= 0
        E = E + p(i)*log2(1/p(i));
    end
end
}
\end{lstlisting}

\end{document}
  • 1
    Welcome to TeX.SX! – user31729 Jan 14 '15 at 17:16
  • 1
    Thank you very much, your answer has solved my problem! I am new here and i didn't know that i can mark your answer as acceptable. – Greek Freak Apr 2 '15 at 19:34
  • @GreekFreak No problem! Welcome to TeX.SE. – jub0bs Apr 2 '15 at 20:00
5

You need to

  • activate the English language
  • switch to the document's monospace font (which is Courier, because you've loaded the courier package)

in the value passed to the basicstyle key, like so:

basicstyle=\selectlanguage{english}\ttfamily,

enter image description here

Note that, if Greek was active before the listing, Greek will still be active after the listing, because all the declarations in the value passed to that basicstyle are local to the listing.

\documentclass[12pt]{article}
\usepackage[english,greek]{babel}
\usepackage[utf8x]{inputenc}
\usepackage{graphicx}
\usepackage{fancybox}
\usepackage{verbatim}
\usepackage{kerkis}
\usepackage{float}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{tocloft}
\usepackage{hyperref}
\usepackage{courier}
\usepackage{afterpage}
\usepackage{color}
\usepackage{listings}

\renewcommand{\lstlistingname}{Κώδικας}

\lstset{
%matlab
  backgroundcolor=\color{white},
  basicstyle=\normalsize\selectlanguage{english}\ttfamily,  % COURIER NEW?
  commentstyle=\color{green},    % comment style
  escapeinside={\%*}{*)},
  extendedchars=true,
  frame=single,
  keywordstyle=\color{blue},       % keyword style
  numbers=left,
  numbersep=5pt,
  numberstyle=\normalsize\color{black},
  rulecolor=\color{black},
  stepnumber=1,
  stringstyle=\color{magenta},     % string style
}

\begin{document}

\selectlanguage{greek}
Ελληνικά: \textlatin{this word means greek}

\selectlanguage{english}
Now I write in english

\selectlanguage{greek}
Ελληνικά

%\selectlanguage{english}???

\begin{lstlisting}[caption = \textlatin{example1.m}]
{
% Entropy
E = 0;
for i = 1:J
    if p(i) ~= 0
        E = E + p(i)*log2(1/p(i));
    end
end
}
\end{lstlisting}

Ελληνικά: \textlatin{this word means greek}

\selectlanguage{english}
Now I write in english

\selectlanguage{greek}
Ελληνικά

\end{document}

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.