2

Can anyone spot my mistake in my LaTeX code? It's my first time working with LaTeX, so I am not all to familiar with how it all works. I get an error at line 38 for my code with the following description "LaTeX Error: Unicode character − (U+2212)".

My code is the following:

\documentclass[a4paper, 12pt]{article}

\usepackage[utf8]{inputenc} \usepackage{tgtermes} \usepackage{fouriernc} \usepackage[T1]{fontenc} \usepackage[margin=3cm]{geometry}

\usepackage{amssymb} \usepackage{amsmath}

\usepackage{enumerate} \usepackage{verbatim} \usepackage{hyperref} \hypersetup{ colorlinks=true, linkcolor=red,
urlcolor=red, } \usepackage{listings} \newcommand{\N}{\mathbb{N}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\Q}{\mathbb{Q}} \newcommand{\R}{\mathbb{R}}

\title{1. Handin\{\large \textsc{Algoritmer og datastrukturer}}} \author{Gustav Næsborg Eskesen og Casper Kejser} \date{September 11th, 2023}

\begin{document}

\maketitle

\section{Problem 1.8: Searching an infinite sorted list *} \underline{\textbf{\emph{Problem description:}}} \ \emph{Assume we have an infinite long increasing sequence of real values $x_1<x_2<x_3<\cdot\cdot\cdot$ and we want to find the position of a real value $y>x_1$ in this list. More specifically, we want to find the index $d$ where $x_d-1<y\le x_d$, i.e. $x_d=y$ if $y$ is in the list, and otherwise $x_d$ is the successor of $y$ in the list. E.g. for the sequence $3 5 7 9 11 17 19 23 31 33 37 . . .$ and $y = 11$ we have $d = 5$, since $y = x_5 = 11$, whereas for $y = 24$ we have $d = 9$, since $23 = x_8 < y \le x_9 = 31$. We assume the sequence to be divergent, i.e. there always exists such a $d$. Describe an algorithm to find the index $d$, where the number of comparisons performed between $y$ and the $x_i$s is a function of the final value of $d$. What is the most efficient algorithm (fewest number of comparisons performed) you can find? Argue that your algorithm finds the correct index $d$, such that either $y = x_d or x_d−1 < y < x_d$. Analyze your algorithm — how many comparisons does your algorithm perform as a function of $d$?} \ \ We will try solving this problem using binary search. As we know binary search needs a list of numbers, which has an end, because it is not possible for us to go to the middle of an infinte number list. So we will start from the start of the list, and take a sequence of numbers from the list. We will then check the highest number of the list and if it is under y we will double the list size, and then continue to do this until we have a list where y is in it. So if we take a set of numbers from the start of the list that goes from $x_1 + x_2 ..... x_b$. We then check that $x_b \geq y$. If not then we double our search amount. Such that $x_{2b}$. And we continue doing this $x_{((2b)2)2.....n}$ until $x_n \geq y$. We can rewrite this to $x_{b2^n}$. And if we want to make it even more digestible we can set b as 1, such that we can say $2^n$. \ \ We extend our list of numbers by $2^n$. Until this $x_{2^n} \geq y$ is true. When this is true we can take the list of numbers we have and start performing binary search, unless $x_{2^n} = y$, then we have found y and can end the search. We will from now call y for its index d, because that is what we are suppose to find in the assignment description. We now know that d exist in this list $2^{n-1} < d \leq 2^n$. This means that the list length is $2^n - 2^{n-1}$. We can reduce this to $2^{n-1}$. \ \ Using the function from thereom 6 from literature [6]. We know now that our algorithm will in the worst case scenario make $\lceil log_2 ({2^{n-1}} - 1) \rceil$ comparisons before it finds index d.

\end{document}

campa
  • 31,130

2 Answers2

1

It's the formula almost at the end of the paragraph on line 38, $y = x_d or x_d-1 < y < x_d$. The symbol - is not the usual. Is your file encoded in utf8?

  • Hi Valentín,

    Thank you for your answer. And yes, it is encoded in UTF8. Does that make any difference or?

    – Casper Kejser Sep 11 '23 at 15:15
  • 1
    Yes and no, you are telling LaTeX that the file goes UTF-8 with \usepackage[utf8]{inputenc} but then it seems that character is not included in the package. It is complicated to insert that character by keyboard if the encode of the file is OK. You got it from a copy&paste of the formula or something, probably. – Valentín Gutiérrez Gil Sep 11 '23 at 18:43
1

Let me re-state some of the things that were already mentioned above, either as comments or answer:

The offending character is utf-8 code 2212, which is the mathematical minus sign. It may look like a hyphen (depends on the font), but performs differently. Character utf-8 002D is the standard hyphen, which can do double-duty as a minus. Apparently, the LaTeX math processing expects an ordinary hyphen there, which it will interpret as a minus. It is confused by the actual utf-8 minus sign. This is surprising to me.

That can be detected by loading the plain text into a hex editor. You will see that where hex code 2D is expected (hyphen), instead there are the three codes E2 88 92 which is utf-8 for character 2212 (mathematical minus).

Now, in general you should utf-8 unless there is a good reason to do otherwise, such as if you are using older LaTeX code. But that is not the issue here.

Another possible confusion, not in this code, is when the non-breaking space character A0 is inserted, instead of ordinary space, in the plain text file. They both look like a space, but may be processed differently by LaTeX.

As for using \\ for line breaks: Sure, you can do that. But when you use \\ it means that the line will not be justified, and the following text is part of the same continued paragraph (no indent there). That is probably not what you mean to do. So instead, you may use a blank line of plain text between paragraphs, or explicitly end a paragraph using \par code.

rallg
  • 2,379