0

How does one include the Hyphen/minus sign into LaTex in the code listing?

e.g. '-' in real toff = t - 10;

This is shown in the code below (with choice of document class etc. - initial conditions shown first.)

%********Initial conditions***********

%\documentclass[12pt]{scrartcl}
\documentclass[12pt]{amsart}
%\documentclass[12pt,oneside]{amsart}
\usepackage[T1]{fontenc}
%\documentclass{article}

%\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}

\usepackage{geometry}
\usepackage{graphicx}
\usepackage[version=4]{mhchem}
\usepackage{breqn}
\usepackage[colorlinks=true, citecolor=blue, linkcolor=blue, urlcolor=blue]{hyperref}
\newgeometry{asymmetric, centering}
\usepackage [english]{babel}
\usepackage [autostyle, english = american]{csquotes}
%\MakeOuterQuote{"}
%% show notes and keys in the draft mode %%%%%%%%%
\usepackage{ifdraft}
\ifoptionfinal{
\usepackage[disable]{todonotes}
}{
%\usepackage[norefs, nocites]{refcheck} % commented out bjd 5.3.2019 - refcheck prints labels in pdf doc as does showkeys
\usepackage{soul}
%\providecommand*\showkeyslabelformat[1]{\normalfont\tiny\ttfamily#1} % commented out bjd 5.3.2019 - refcheck prints labels in pdf doc as does showkeys
%\usepackage[notref, notcite]{showkeys} % commented out bjd 5.3.2019 - refcheck prints labels in pdf doc as does showkeys
\usepackage[bordercolor=white, color=white]{todonotes}
}
\usepackage{xfrac}
\usepackage{amsmath, amsthm, amssymb, amsfonts} 
\usepackage{upgreek} 
\usepackage{listings}
%\lstset{language=C++, commentstyle={\rmfamily\catcode`\$=11}, columns=flexible,texcl}
\lstloadlanguages{[5.2]Mathematica}

\lstset{upquote=true, showstringspaces=false}

%------------BJD 3.3.2019-----------------------
\usepackage{bm}
\newcommand{\uveci}{{\bm{\hat{\textnormal{\bfseries\i}}}}}
\newcommand{\uvecj}{{\bm{\hat{\textnormal{\bfseries\j}}}}}
\DeclareRobustCommand{\uvec}[1]{{%
        \ifcsname uvec#1\endcsname
        \csname uvec#1\endcsname
        \else
        \bm{\hat{\mathbf{#1}}}%
        \fi
    }}
%------------BJD 15.8.2018-----------------------
\makeatletter
\def\@settitle{\begin{center}%
        \baselineskip14\p@\relax
        \bfseries
        \uppercasenonmath\@title
        \@title
        \ifx\@subtitle\@empty\else
        \\[1ex]\uppercasenonmath\@subtitle
        \footnotesize\mdseries\@subtitle
        \fi
    \end{center}%
}
\def\subtitle#1{\gdef\@subtitle{#1}}
\def\@subtitle{}
\makeatother
%-------------------------------------------------
%******BJD change****************
%\documentclass{article}
%\usepackage{graphicx}
%\graphicspath{ {/home/user/images/} }
%\graphicspath{ {/home/bjd/Downloads/} }
%******BJD change end****************
\newcommand{\HOX}[1]{\todo[noline, size=\footnotesize]{#1}}
\newcommand{\TODO}[1]{\hfill\todo[inline, bordercolor=black, color=green!10]{#1}}
%% HACK from Sec. 1.6.4 of the manual of todonotes package
\makeatletter\providecommand\@dotsep{5}\def\listtodoname{List of Todos}\def\listoftodos{\hypersetup{linkcolor=black}\@starttoc{tdo}\listtodoname\hypersetup{linkcolor=blue}}\makeatother
%%%%%%%%%%%%%%%%%%

%% numbering %%%%%%%%%
\newtheorem{lemma}{Lemma}
\newtheorem{proposition}{Proposition}
\newtheorem{theorem}{Theorem}
\newtheorem{corollary}{Corollary}
\newtheorem{definition}{Definition}
\theoremstyle{remark} 
\newtheorem{example}{Example}
\newtheorem{remark}{Remark}

%% definitions %%%%%%%%% 
\def\C{\mathbb C}
\def\R{\mathbb R}
\def\Q{\mathbb Q}
\def\Z{\mathbb Z}
\def\N{\mathbb N}

\def\p{\partial}
\DeclareMathOperator{\supp}{supp}
\newcommand{\pair}[1]{\left\langle #1 \right\rangle}
\newcommand{\norm}[1]{\left\|#1 \right\|}
%\newcommand{\dd}[1]{\mathrm{d}#1}
%\def\inter{\text{int}}
%\let\dim\relax
%\DeclareMathOperator{\dim}{dim}

%********Initial conditions end***********

\begin{lstlisting}[basicstyle=\tiny, language=c++, caption={ Model G in one dimension \href{https://github.com/bodavid/model_g_1d}{c++ Crank Nicolson solver} - Note: listings miss out minus/hyphen signs.}, label={lst:list2}
]

#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <stdexcept>
using namespace std;

typedef double real;

const real dx = 1, dy = 12, a = 14, b = 29,
  g = 0.1, p = 1, q = 1, s = 0, u = 0, w = 0;

const real G0 = (a+g*w)/(q-g*p);
const real X0 = (p*a+q*w)/(q-g*p);
const real Y0 = (s*X0*X0+b)/(X0*X0+u)*X0;

const int Length = 100; // Size of space in system units
const int Time = 100; // Duration of time in system units
const int Nx = 8; // Number of computational cells per unit length.
const int Nt = 180; // Number of computational cells per unit time.
const int Mx = Nx*Length; // Size of space in computational units
const int Mt = Nt*Time; // Duration of time in computational units

enum { G, X, Y };
const real vacuum[] = { G0, X0, Y0 };
const real pcn = real(Nx*Nx)/(2*Nt); // pre-Crank-Nicolson coefficient
const real d[] = { pcn, dx*pcn, dy*pcn }; // Crank-Nicolson coefficients
// 2: current and next iteration
// 3: phi_G, phi_X, phi_Y
// Mx+2: Mx computational cells in entire spatial dimension, plus 0 on each end
real space[2][3][Mx+2];

void init()
{
  for( int i=0 ; i<2 ; ++i )
    for( int j=0 ; j<3 ; ++j )
      for( int k=0 ; k<Mx+2 ; ++k ) space[i][j][k] = 0;
}

real chi( const real &x, const real &t )
{
  real xoff = x - real(Length)/2;
  real toff = t - 10;
  return -exp(-xoff*xoff/2)*exp(-toff*toff/18);
}

real reaction( const real (*pcurr)[Mx+2], int R, int j, int t )
{
....

\end{lstlisting}

Example LaTex output showing code listing (misses minus/hyphen sign) as shown.

enter image description here

  • 1
    Related: https://tex.stackexchange.com/questions/6827/minus-signs-vanish-with-listings-and-breqn-any-solutions The accepted answer in this question does work with the MWE provided. – daleif Mar 20 '20 at 10:24

0 Answers0