0

this question builds from questions answered by Ignasi and Paul Gessler. I would like to know how I can go about changing the font in the Matlab-prettifier environment using this WinEdt QuickGuide template. Here is my code:

\documentclass[%
   ,paper=a4     
   ,pagesize=auto
   ,BCOR=0.75cm  
%   ,oneside     
   ,DIV=10       
%   ,DIV=calc   
   ,numbers=noenddot 
   ,captions=heading 
   ,captions=nooneline 
   ,listof=totoc 
   ,bibliography=totoc 
   ,index=totoc  
   ,headings=normal 
%   ,headings=small  
   ,USenglish,   
]{scrbook}
%

\usepackage{etex}
\reserveinserts{28}

\usepackage[refsection=chapter,defernumbers=true,sorting=none,sortcites=true,autopunct=true,babel=hyphen,hyperref=true,abbreviate=false,backref=true,backend=biber]{biblatex}

\newcommand*{\VersionInformation}{%
   \centering{%
      Controls\par
      Typeset with \LaTeXe\ and \KOMAScript\ class\par
      using \textrm{Palatino} and \textsf{Bera} fonts.\par
   {\scriptsize generated: \today\par}}
}
%

\usepackage[%
             ,headsepline
             ,footsepline
             ,plainfootsepline
            ]{scrpage2}
\pagestyle{scrheadings}

\setheadwidth[-7.5mm]{1.175\textwidth}
%
% headline color
\setheadsepline{.5pt}[\color{Navy}]
\setfootsepline{.5pt}[\color{Navy}]
%

\newcommand*{\ORIGchapterheadstartvskip}{}%
% 2nd save the original definition to the new command
\let\ORIGchapterheadstartvskip=\chapterheadstartvskip
% 3rd redefine the command using the saved original command
\renewcommand*{\chapterheadstartvskip}{%
  \ORIGchapterheadstartvskip
  {\color{Navy}%
    \setlength{\parskip}{0pt}%
    \noindent\rule[.3\baselineskip]{\linewidth}{1pt}\par
  }%
}
% see above
\newcommand*{\ORIGchapterheadendvskip}{}%
\let\ORIGchapterheadendvskip=\chapterheadendvskip
\renewcommand*{\chapterheadendvskip}{%
  {\color{Navy}%
    \setlength{\parskip}{0pt}%
    \noindent\rule[.3\baselineskip]{\linewidth}{1pt}\par
  }%
  \ORIGchapterheadendvskip
}
%

\usepackage{chapterthumb}
\renewcommand*{\chapterthumbboxcolor}{Navy}
\renewcommand*{\chapterthumbfont}{\normalfont\Large\sffamily\bfseries}
\renewcommand{\chapterthumbwidth}{.125\paperheight}
   \lohead[\putchapterthumb]{\putchapterthumb}
   \addtokomafont{chapterthumb}{\small{\bfseries}}

   \addtokomafont{captionlabel}{\bfseries\color{Navy}}
   \addtokomafont{caption}{\color{Navy}}
   \addtokomafont{chapter}{\color{Navy}}
   \addtokomafont{section}{\color{Navy}}
   \addtokomafont{subsection}{\color{Navy}}
   \addtokomafont{subsubsection}{\color{Navy}}
   \addtokomafont{paragraph}{\color{Navy}}
   \addtokomafont{minisec}{\color{Navy}}
   \addtokomafont{pageheadfoot}{\color{Navy}}
   \addtokomafont{pagefoot}{\color{Navy}}
   \addtokomafont{pagenumber}{\color{Navy}}
   \addtokomafont{footnoterule}{\color{Navy}}

\usepackage{marvosym}

\usepackage{%
            ,fix-cm     
%            ,fix2col    
            ,etex      
            ,thinsp     
            ,xspace    
%            ,ifthen
            ,calc       
            }
% --------------------------------------------------------------------
%
\usepackage{babel}      
%
% line break after /:
    \defineshorthand{"/}{\slash}
%
\usepackage[T1]{fontenc}        % EC fonts
\usepackage[utf8]{inputenc}     % coding UTF-8

\usepackage[%
            ,showerrors%
            ,svgnames%
            ,table% colors in tables
           ]{xcolor}
%
% --------------------------------------------------------------------
\usepackage{%
            ,graphicx         
            ,rotating           
            ,ltablex           
%                              
%                               
            ,dcolumn           
            ,booktabs           
           }

    \usepackage{siunitx}

    \renewcommand{\floatpagefraction}{0.75}% Standard = 0.5
    \renewcommand{\textfraction}{0.15}% Standard = 0.2
%

    \renewcommand{\topfraction}{0.8}% Standard = 0.7
    \setcounter{topnumber}{3} % vorher: 2
    \setcounter{bottomnumber}{1} % vorher: 1
    \setcounter{totalnumber}{5} % vorher: 3
%
    \renewcommand{\dbltopfraction}{.8}% Standard = 0.7
    \renewcommand{\dblfloatpagefraction}{.6}% Standard = 0.5
%
% --------------------------------------------------------------------

    \tolerance 1414
    \hbadness 1414
    \emergencystretch 1.5em
    \hfuzz 0.3pt
    \widowpenalty = 2500
    \displaywidowpenalty = 2500
    \vfuzz \hfuzz
    \raggedbottom
\DeclareOption{tanz}{\raggedbottom}
%

    \ProcessOptions\relax
  \deffootnote{2.25em}{1.75em}{\thefootnotemark.\enspace}

\usepackage{expl3,xparse}
%
\ExplSyntaxOn
%
\cs_new_eq:Nc \emph_old:n { emph~ } 
\cs_new_eq:NN \emph_braces:n \textup 
%
\cs_new:Npn \emph_new:n #1 {
  \tl_set:Nn \l_emph_tl {#1}
  \tl_replace_all:Nnn \l_emph_tl {(}{\emph_braces:n{(}}
  \tl_replace_all:Nnn \l_emph_tl {)}{\emph_braces:n{)}}
  \tl_replace_all:Nnn \l_emph_tl {[}{\emph_braces:n{[}}
  \tl_replace_all:Nnn \l_emph_tl {]}{\emph_braces:n{]}}
  \exp_args:NV \emph_old:n \l_emph_tl
}
%
\RenewDocumentCommand {\emph} {sm} {
  \IfBooleanTF {#1} {\emph_old:n {#2}} {\emph_new:n {#2}}
}
%
\AtEveryCitekey{\RenewDocumentCommand{\emph}{m}{%
    \emph_old:n {#1}}
}
\AtBeginBibliography{\RenewDocumentCommand{\emph}{m}{%
    \emph_old:n {#1}}
}
\ExplSyntaxOff% syntax of expl3 on

\usepackage[iso]{isodate}


\PassOptionsToPackage{hyphens}{url}

\ifpdfoutput{% needs a KOMA-Script class!!!

   \usepackage[%
               ,final
               ,kerning
               ,babel
               ,protrusion=true
               ,expansion=true % better typography, but with much larger PDF file
               ,tracking=true
              ]{microtype}
   \DeclareMicrotypeSet*[tracking]{my}{ font = */*/*/sc/* }%
      \SetTracking{ encoding = *, shape = sc }{ 45 }
%

   \usepackage[%
               ,pdftex%
               ,plainpages=false%
               ,colorlinks%
               ,linkcolor={Navy}%
               ,citecolor={Navy}%
               ,linktocpage={true}%
               ,breaklinks%
               ,pdfcreator={pdflatex}%
               ,pdfstartview=FitH
               ,bookmarksopen=true% book marks in PDF reader
               ]{hyperref}
%
\usepackage{bookmark}% bookmark cleanup
}%
{%
   \usepackage[%
               ,hypertex%
               ,linktocpage={true}%
               ,breaklinks%
               ]{hyperref}
%
% optimized margin alignment with microtype:
   \usepackage[%
               ,final
               ,DVIoutput
               ,kerning
               ,babel
               ,protrusion=true
              ]{microtype}
}
%
\usepackage[figure]{hypcap} % links to the upper edge of floating environments
%
%% ============================================================================
% extended handling of figures
\usepackage[caption=false]{subfig}
%
%% ============================================================================
% intelligent cross references
\usepackage{cleveref}
%% ============================================================================
% Fonts
\usepackage{textcomp} 
%

    \linespread{1.05}\selectfont      
    \usepackage[scaled=.88]{beramono} 
    \usepackage[scaled=.86]{berasans} 
    \usepackage[sc,osf]{mathpazo}     

\usepackage{amsmath}
%

   \usepackage{fixmath}


   \typearea[current]{last}    

\usepackage{textcomp} 
\usepackage{eurosym}  
\catcode`\=13
\def{•} 
%% ============================================================================
\usepackage{lscape} 
\usepackage[active,new,noold,marker]{xrcs}
%% ============================================================================

\newcommand{\WinEdtCmd}[1]{\textcolor{CornflowerBlue}{\textsf{#1}}}
\newcommand{\WinEdtKey}[1]{\textcolor{LightSeaGreen}{\textsf{#1}}}
\newcommand{\WinEdtFile}[1]{\textcolor{DarkSlateGray}{\texttt{#1}}}

%========================================================================================
%   Bibliography
%========================================================================================

%\addbibresource{References}% BibTeX
 \defbibheading{bibempty}{}
 \newcommand*{\refname}{Bibliography}
% -----------------------------------------------------------------------------
% Matlab Arguments
% -----------------------------------------------------------------------------
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{filecontents}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\usepackage[numbered,framed]{matlab-prettifier}
%\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}

%\newfontfamily{\lstconsolas}{Consolas}

\newcommand\ph\mlplaceholder
\makeatletter
\renewcommand\phOpDelim@mlpr{$\langle$}
\renewcommand\phClDelim@mlpr{$\rangle$}
\makeatother
\renewcommand{\lstlistingname}{Matlab Algorithm}
\renewcommand{\lstlistlistingname}{List of \lstlistingname s}

\begin{document}

\KOMAoptions{cleardoublepage=plain}
\addtokomafont{caption}{\centering}

\frontmatter

\subject{\ }
\title{Controls}
\subtitle{Problem Set 1}
\author{\textsc{Author}}
\date{2015}
%
\lowertitleback{\VersionInformation}

\maketitle

\tableofcontents

\listoffigures

\lstlistoflistings
\mainmatter
\chapter{Problem 1}
\lstinputlisting[style=Matlab-editor,caption=Sample code from Matlab]{sample.m}

%\include{Chapters/Solution1}
\begin{lstlisting}[
  style=Matlab-editor,
  basicstyle=\mlttfamily,
  escapechar=`,
  caption={For educational purposes},
]
% example of while loop using placeholders
while `\ph{condition}`
  if `\ph{something-bad-happens}`
    break
  else
    x = 2015 + 10098 % do something useful
  end
  % do more things
end
\end{lstlisting}

%% ============================================================================

\backmatter
\lohead[]{}

\printbibheading[heading=bibintoc,title=\bibname]
\printbibliography[heading=subbibintoc,type=book,prefixnumbers={B},title={Books}]
\printbibliography[heading=subbibintoc,type=article,prefixnumbers={A},title={Articles}]
\printbibliography[heading=subbibintoc,type=online,prefixnumbers={O},title={Online}]

\end{document}

However, when I try to implement

\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption} \newfontfamily{\lstconsolas}{Consolas}

I get errors using PDF latex. Ignasi said that I cannot use PDFLaTeX for this format. How can I change the Matlab font here?

Joe
  • 9,080
  • If you want to use fontspec, you have to use XeLaTeX or LuaLaTeX, there's no getting around that. But there are other ways to select fonts than fontspec. Which type of solution are you looking for? – Paul Gessler Jun 15 '15 at 20:07
  • @PaulGessler Consolas font – Joe Jun 16 '15 at 02:06
  • That doesn't answer my question at all: do you want a solution using fontspec (if so, you need to use xelatex or lualatex) or do you want some way to use Consolas in pdflatex? – Paul Gessler Jun 16 '15 at 02:33
  • @PaulGessler, I would like to use Consolas in pdflatex. I would like to get Consolas to work with my MWE above. – Joe Jun 16 '15 at 18:04
  • Assuming that you have the font in opentype format and that the licence allows you to do so, you can convert the font using something like otftotfm. pdfTeX requires a number of files in order to use the font: .tfm files, an addition to the .map files, the fonts themselves etc. For LaTeX, you need .fd files and, ideally, a .sty file as well. In some cases, .vf files may also be either required or easier to work with. How badly do you want to use this font and how badly do you want to stick to pdfTeX?! – cfr Jun 18 '15 at 01:58
  • @cfr, I have to use pdflatex, but i just need a font to use in matlab-prettifier that shows up a differentation between a zero and a letter 'o'. Can I use the insconsolata font then in pdflatex if using consolas is difficult? – Joe Jun 18 '15 at 02:05
  • @Joe Oh, yes. Inconsolata shouldn't be a problem. See The LaTeX Font Catalogue for basic instructions. The package documentation provides further details. – cfr Jun 18 '15 at 02:36
  • @Joe If you could post a minimal example, it would be possible to give you an answer showing how to do it. However, right now, I can't even get your code to compile. Note that you are loading packages multiple times with different options which is probably not helping any - at the very least, it makes it harder to see what is going on. – cfr Jun 18 '15 at 20:45

1 Answers1

3

Note that it is not possible to work with the code you posted as is as it does not compile and relies on non-standard packages. When I tried to compile it, I got countless compilation errors. When I fixed those, I got complaints about missing packages. When I removed those, more errors.

Do you want something like this?

Inconsolata for code listings

As far as I can tell, answers to your earlier questions have already explained how to do this.

Here's a minimised version of your code which produces the above output:

\documentclass[paper=a4,pagesize=auto,BCOR=0.75cm,DIV=10,numbers=noenddot,captions=heading,captions=nooneline,listof=totoc,bibliography=totoc,index=totoc,headings=normal,USenglish]{scrbook}

\usepackage{babel}

% line break after /:
\defineshorthand{"/}{\slash}

\usepackage[T1]{fontenc}        % EC fonts
\usepackage[utf8]{inputenc}     % coding UTF-8

\usepackage[showerrors,svgnames,table]{xcolor}
\definecolor{ocre}{RGB}{243,102,25}

% Fonts
\linespread{1.05}\selectfont
% \usepackage[scaled=.88]{beramono}
\usepackage{inconsolata}% use Inconsolata as typewriter font
\usepackage[scaled=.86]{berasans}
\usepackage[sc,osf]{mathpazo}
\usepackage{amsmath}
\usepackage{fixmath}

% -----------------------------------------------------------------------------
% Matlab Arguments
% -----------------------------------------------------------------------------
\definecolor{ocre}{RGB}{243,102,25}
\usepackage{filecontents}
\begin{filecontents*}{sample.m}
% create a file for output
!touch testFile.txt
fid = fopen('testFile.text', 'w')
for i=1:10
  fprintf(fid,'%6.2f \n', i);
end
\end{filecontents*}

\usepackage[numbered,framed]{matlab-prettifier}

\usepackage[font={color=ocre,bf},figurename=Fig.,labelfont={it}]{caption}

\newcommand\ph\mlplaceholder
\makeatletter
\renewcommand\phOpDelim@mlpr{$\langle$}
\renewcommand\phClDelim@mlpr{$\rangle$}
\makeatother
\renewcommand{\lstlistingname}{Matlab Algorithm}

\begin{document}

\addtokomafont{caption}{\centering}

\lstinputlisting[style=Matlab-editor,caption=Sample code from Matlab]{sample.m}

\begin{lstlisting}[
style=Matlab-editor,
basicstyle=\mlttfamily,
escapechar=`,
caption={For educational purposes},
]
% example of while loop using placeholders
while `\ph{condition}`
  if `\ph{something-bad-happens}`
    break
  else
    x = 2015 + 10098 % do something useful
  end
% do more things
end
\end{lstlisting}

\end{document}
cfr
  • 198,882
  • When I tried to run your code, I got the error miktex-makemf: The ts1-zi4r source file could not be found.

    Running ttf2pk.exe... miktex-makepk: PK font ts1-zi4r could not be created.

    !pdfTeX error: pdflatex.exe (file ts1-zi4r): Font ts1-zi4r at 657 not found ==> Fatal error occurred, no output PDF file produced! Is there a package that I have to install for this to work?

    – Joe Jun 18 '15 at 21:40
  • @Joe If it did not complain about a missing package, then likely the package is installed and this is just MikTeX's usually flakiness when it comes to updating the font stuff. The package is inconsolata. See this answer for how to check. (It is about a different set of fonts, but the idea is the same.) – cfr Jun 18 '15 at 21:44
  • @Joe Good!! ;). – cfr Jun 18 '15 at 22:02
  • I was comparing the inconsolata font and the consolas font and I seem to prefer the consolas font. Will it be really involved to retrofit the consolas font to work with your code above? Like to include a third lstlisting to use the consolas font? – Joe Jun 18 '15 at 22:18
  • @Joe With latex/pdflatex? Yes. With xelatex/lualatex? No. The problem in the first case is that no font support package exists. So you would have to create all of the font support yourself. There are tools to help with some of this but it takes patience - it involves a significant amount of time and energy. It is fiddly and time-consuming. Take a look at my CTAN packages. All of those packages provide (pdf)LaTeX support for fonts in one way or another. (I'm only pointing to mine as I know them. There are lots of others, too.) – cfr Jun 18 '15 at 22:49
  • @Joe That is, it is not just a question of changing the code in your document. You cannot use Consolas with (pdf)TeX that way. You have to create a font support package for it and install it. Then you change your document code. Changing the document is the easy bit. If Xe/LuaTeX is an option, it is entirely different. Then you can use Consolas just by changing the code of your document. But not for (pdf)TeX. (pdf)TeX needs TeX Font Metric files (.tfm), maps (.map) etc. The Font Installation Guide explains one way to go (e.g. cfr-lm). otftotfm provides another (e.g. berenisadf). – cfr Jun 18 '15 at 22:54