0

I used a £ sign in one of my axis labels in my MATLAB code but it does not show in the compiler.

My LaTeX code is:

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}

\title{BENG0019: Engineering Mathematics in Finance \\[10pt] \textbf{Assignment 2}}
\author{Michael Dodman 18020495}
\date{}

\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{titling}
\setlength{\droptitle}{-4em}

\usepackage{parskip}

\usepackage{geometry}
\geometry{portrait, margin=0.7in}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\rhead{18020495}
\lhead{Michael Dodman}
\cfoot{Page \thepage}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}
\setlength{\headheight}{14pt}

\UseRawInputEncoding
%\usepackage[framed,numbered,autolinebreaks]{mcode}

\usepackage[framed,numbered]{matlab-prettifier}
\lstset{
  style      = Matlab-editor,
  basicstyle = \fontfamily{pcr}\selectfont\footnotesize, % if you want to use Courier
}



\begin{document}
\maketitle \thispagestyle{fancy}

\newpage \section*{Appendix A}
\begin{lstlisting}[style=Matlab-editor]
% Input X data into a 1 x 10 array
x = [85 105 122 143 162 182 203 224 242 262];

% Input each column of Y data into a 1 x 10 array of arrays
y = {[55 62 61 73 76]; [64 72 73 82 87 88]; [79 84 90 94 99]; [80 93 95 103 105 113 115]; [102 107 110 115 113 125]; [110 115 120 130 132 140]; [120 136 140 144 143]; [135 137 140 152 152 160 162]; [137 145 155 164 174 189]; [152 155 173 171 182 182 192]};

% Plotting each column of Y data individually in turn,in a  for loop and
% adding to the existing graph. Plotting filled circles, size 11pt. 
% For loop ends when all data is plotted.
for i = 1:numel(x)
    scatter(ones(1,numel(y{i}))*x(i), y{i}, 11,'filled', 'o' )
    hold on
end

% Adding a grid and axis labels for clarity
grid on
xlabel('Weekly Income/ \pounds ')
ylabel('Weekly Consumption Expenditure/ \pounds ')
\end{lstlisting}

\newpage \section*{Appendix B}

\end{document}

However, the output is:

enter image description here

As you can see, the \pounds just does not show up?

Any help with this?

Link for log file:

https://drive.google.com/file/d/1xCegXUrg_OhAPhvr1NvznKztweGGUuZI/view?usp=sharing

imnothere
  • 14,215
  • Please make sure your example is self contained. We do not have that image and it is probably not even relevant. Unrelated \end{figure}\\ does not make any sense at all since figure is a float. Additionally don't invent your own headlines use \subsubsection*{....} instead of those \textbf headlines. Bonus added spacing around them. – daleif Mar 23 '20 at 17:58
  • 1
    Additionally I see \pounds in the example after I've removed the image. Might be an idea if you provided the log file for this exact example. – daleif Mar 23 '20 at 18:00
  • Thank you for the advice, I have added a link to the log file – Michael Mar 23 '20 at 18:29
  • I don't see anything special on it. Have you tried downloading the PDF and trying a different PDF viewer? Just to rule out the overleaf viewer/your browser. – daleif Mar 23 '20 at 18:32
  • (I'm on support staff at Overleaf.) I see \pounds verbatim in the output, I'm not sure what would be causing it to display different for you. Here's a test: https://www.overleaf.com/read/hbkspnyrzdty I've removed the personal information and unnecessary code but it was the same listing output before I did that. If you still see a problem, write us at support@overleaf.com with your project's URL and we can take a look at your whole project to see what's gone wrong. – Paul Gessler Mar 23 '20 at 19:03

1 Answers1

2

(I'm yet another Overleaf support staff)

Am I understanding correctly that you want the listings in the output PDF to display £ not \pounds in verbatim? The way you wrote your code here, it seemed that you had wanted \pounds.

To get a £ in your lstlistings output, you actually have a similar scenario as How to set UTF8 in a lstlisting? (Error received). You have three choices:

Escape-to-LaTeX for \pounds

Use the listings package's "escape" mechanism, so that \pounds is interpreted as a LaTeX command. (It may not be syntax-highlighted though)

%% For escapechar, choose a character that's not used in your code snippet! 
\begin{lstlisting}[style=Matlab-editor,escapechar=|]
xlabel('Weekly Income/ |\pounds|')
\end{lstlisting}

"Literate" £ away; then write £ directly

With this approach, you define what listings should do (use \pounds instead) when it encounters a £.

\begin{lstlisting}[style=Matlab-editor,
  extendedchars,literate={£}{{\pounds}}1]
xlabel('Weekly Income/ £')
\end{lstlisting}

Compile with XeLaTeX or LuaLaTeX; write £ directly

Set your project to compile with XeLaTeX or LuaLaTeX (on Overleaf, click on the menu icon above the file tree menu, and change the "Compiler" setting). These compilers handle UTF-8 natively; so this will work right away:

\begin{lstlisting}[style=Matlab-editor]
xlabel('Weekly Income/ £')
\end{lstlisting}

Sample output of the above three approaches:

Different outputs using escaping; "literating"; and XeLaTeX/LuaLaTeX to include a £ symbol in a lstlisting

imnothere
  • 14,215