2
\documentclass{article}
\usepackage[left=3cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true,numbers=left,mathescape}
\begin{document}
\begin{lstlisting}
clear all;
clc;
j=1;
if j~=1
    fprintf('j not equal to 1\n');
end
\end{lstlisting}
\end{document}

Why the tilde symbol in if j~=1 on the top? How to make it in the center on the line text? enter image description here

2 Answers2

4

In fact the solution comes from https://tex.stackexchange.com/a/130513/231952, but it is part contained in a comment, so I thought it appropriate to summarize it here. (Other solutions here: How to insert a nice tilde in a lstlisting?)

\documentclass{article}
\usepackage[left=3cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true,numbers=left,mathescape}
\usepackage{url}
\usepackage{listings}
\lstset{literate={~}{{\raisebox{-.25em}{\textasciitilde}}}{1}}

\begin{document} \begin{lstlisting} clear all; clc; j=1; if j~=1 fprintf('j not equal to 1\n'); end \end{lstlisting} \end{document}

enter image description here

Ivan
  • 4,368
0

THIS?

\documentclass{article}
\usepackage[left=3cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{listings}
\lstset{basicstyle=\ttfamily\footnotesize,breaklines=true,numbers=left,mathescape}
\usepackage{url}
\usepackage{listings}
\lstset{literate={~}{{\raisebox{-.25em}{\textasciitilde}}}{1}}
\begin{document}
 \begin{lstlisting}
 clear all;
 clc;
 j=1;
 if j\~=1
 fprintf('j not equal to 1\n');
 end
 \end{lstlisting}
\end{document}
Nick B
  • 831