1

I am doing homework for an algorithm class, the main text should be times new roman and I need to change the font every time I write pseudo code, so I set up a new environment called pc. However, I cannot set some of the elements in the pseudocode to be bold with \textbf{} command. This is the main problem. I am new to latex and StackExchange, please let me know if you have any questions regarding my question.

The following is part of my code, all three \textbf{} commands are not working:

\documentclass[12pt]{article} %insert [fleqn] here if you want eqnarray to put things to the left of the page, rather than centered
\usepackage{fontspec}
\setmainfont{Times New Roman}
\usepackage{tikz}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{fancyhdr}
    \fancyhead[L]{Name: \\ Problem Set 1}
    \fancyhead[R]{Due}
    \fancyfoot{}
    \pagestyle{fancy}
    \cfoot{\thepage}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}    
\newcommand{\set}[1]{\left\{#1\right\}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\F}{\mathbb{F}}
\newcommand{\M}{\mathcal{M}}
\newcommand{\LL}{\mathcal{L}}
\newcommand{\B}{\mathcal{B}}
\newcommand{\abs}[1]{\vert #1 \vert}
\newcommand{\norm}[1]{||#1||}
\newcommand{\bea}[1]{\begin{eqnarray}\label{#1}}
\newcommand{\eea}{\end{eqnarray}}
\newcommand{\ql}{\textquotedblleft}
\newcommand{\ds}{\displaystyle}
\newcommand{\nn}{\nonumber}
\newcommand{\Abs}[1]{\Big\lvert#1\Big\rvert}
\newcommand{\rpm}{\sbox0{$1$}\sbox2{$\scriptstyle\pm$}
  \raise\dimexpr(\ht0-\ht2)/2\relax\box2 }
\newcommand{\converges}{\xrightarrow{}}
\newcommand{\interior}{\text{Int}}
\newcommand{\closure}{\text{cl}}
\newenvironment{pc}{\fontfamily{Courier New}\selectfont}{\par}

\begin{document}
\section*{1}
\begin{pc}
1. \hspace{10pt} \textbf{find\_missing(A,(n-1))}\\
2. \hspace{20pt} \textbf{Input:} Array A of n-1 integers. Array indexing starts at 0.\\
3. \hspace{20pt} \textbf{Output:} The missing integer from A.\\
\end{pc}
\end{document}
6
  • 1
    Can you make a compilable minimal reproducible example so we can see which class and packages you use? Commented Feb 11, 2023 at 16:59
  • It seems you are trying to show some kind of algorithm. Did you check out the various packages for algorithms? The can automatically add line numbers, make keywords bold etc. Commented Feb 11, 2023 at 17:00
  • there is no font family "Courier New" and LaTeX will fall back to a default. Commented Feb 11, 2023 at 17:12
  • Thank you for replying. It does change the font from times to something else looks like Courier New from my problem set...I change the Courier New part to Courier, nothing changes, \textbf still does not work
    – Wenhao Xie
    Commented Feb 11, 2023 at 17:21
  • Thank you for ur suggestion, the packages are very useful for my homework
    – Wenhao Xie
    Commented Feb 11, 2023 at 17:23

1 Answer 1

1

You can use the \newfontfamily command from the fontspec package to define a macro which allows you to easily switch fonts. e.g. \newfontfamily{\courier}{Courier New} and then use \courier in your new environment.

However I have to wonder: wouldn't it make more sense to set a suitable mono font via \setmonofont and then set your algorithm in this font?

% !TeX TS-program = lualatex
\documentclass[12pt]{article} %insert [fleqn] here if you want eqnarray to put things to the left of the page, rather than centered
\usepackage{fontspec}
\setmainfont{Times New Roman}
\setmonofont{Courier New}

\usepackage{tikz}
%\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath,amsthm,amssymb}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{fancyhdr}
    \fancyhead[L]{Name: \\ Problem Set 1}
    \fancyhead[R]{Due}
    \fancyfoot{}
    \pagestyle{fancy}
    \cfoot{\thepage}
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
            \node[shape=circle,draw,inner sep=2pt] (char) {#1};}}    
\newcommand{\set}[1]{\left\{#1\right\}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}
\newcommand{\F}{\mathbb{F}}
\newcommand{\M}{\mathcal{M}}
\newcommand{\LL}{\mathcal{L}}
\newcommand{\B}{\mathcal{B}}
\newcommand{\abs}[1]{\vert #1 \vert}
\newcommand{\norm}[1]{||#1||}
\newcommand{\bea}[1]{\begin{eqnarray}\label{#1}}
\newcommand{\eea}{\end{eqnarray}}
\newcommand{\ql}{\textquotedblleft}
\newcommand{\ds}{\displaystyle}
\newcommand{\nn}{\nonumber}
\newcommand{\Abs}[1]{\Big\lvert#1\Big\rvert}
\newcommand{\rpm}{\sbox0{$1$}\sbox2{$\scriptstyle\pm$}
  \raise\dimexpr(\ht0-\ht2)/2\relax\box2 }
\newcommand{\converges}{\xrightarrow{}}
\newcommand{\interior}{\text{Int}}
\newcommand{\closure}{\text{cl}}
\newenvironment{pc}{\ttfamily}{\par}

 

\begin{document}
\section*{1}

text

\begin{pc}
\noindent 1. \hspace{10pt} \textbf{find\_missing(A,(n-1))}\\
2. \hspace{20pt} \textbf{Input:} Array A of n-1 integers. Array indexing starts at 0.\\
3. \hspace{20pt} \textbf{Output:} The missing integer from A.\\
\end{pc}

text

\end{document}

enter image description here

5
  • You are the greatest!!! Thanks alot.
    – Wenhao Xie
    Commented Feb 11, 2023 at 17:39
  • @WenhaoXie You're welcome! Commented Feb 11, 2023 at 17:40
  • Could you pls explian this "However I have to wonder: wouldn't it make more sense to set a suitable mono font via \setmonofont and then set your algorithm in this font?" Are you asking why I define a new environment? I searched questions regarding changing font on this website last night, someone said it would be more convient to define a new environment if i have to change font a lot of times.
    – Wenhao Xie
    Commented Feb 11, 2023 at 17:47
  • @WenhaoXie No, that's not what I'm asking. I'm asking why you are not using \ttfamily for your new environment and choose which font family should be used for the typewriter font via \setmonofamily{Courier New}. See my updated answer. I think this would make more sense than setting everything in normal font and then switching back and forth between different font families. Commented Feb 11, 2023 at 17:52
  • thanks for introducing me two new commands \ttfamily and \setmonofamily, have a great day.
    – Wenhao Xie
    Commented Feb 11, 2023 at 18:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .