TransWikia.com

How to automatically use typewriter font in arrays?

TeX - LaTeX Asked by NilsK on December 4, 2020

I’m trying to write a script on formal logic. The source code looks something like this:

some text...

[
begin{array}[]{c | c}
p   &   (lnot p) \ hline
1   &   0 \
0   &   1 \
end{array}
]

some more text...

How can I make it, that the text inside the array is written in typewriter font? I tried something like this, which works fine:

[
begin{array}[]{c | c}
texttt{p}   &   texttt{($lnot$ p)} \ hline
texttt{1}   &   texttt{0} \
texttt{0}   &   texttt{1} \
end{array}
]

The problem is, that it is much work and not dynamic. So if I would like to change it back to a normal font, I would need to remove everything myself. Is there an elegant solution to this?

Thank you!

2 Answers

Something like this?

enter image description here

documentclass{article}
usepackage{array} % for 'newcolumntype' macro
newcolumntype{T}{>{ttfamily}c} 

begin{document}
begin{center}
begin{tabular}{ T | T }
p  & ($lnot$p) \ 
hline
1  & 0 \
0  & 1 \
end{tabular}
end{center}
end{document}

Since ttfamily is a text-mode command, I suggest employing a tabular environment rather than an array environment.

Correct answer by Mico on December 4, 2020

I think you might have an XY-problem here.

With many OpenType fonts, you would be able to use the command

addfontfeatures{Numbers={Monospaced,Lining}}

to get fixed-width numbers in a more-attractive font. You might wrap this in an environment, such as:

newenvironment{arraymono}%
  {begingroupaddfontfeatures{Numbers={Monospaced,Lining}}begin{array}}%
  {end{array}endgroup}

You might also find the Unicode figure space, which is exactly as wide as a tabular digit, useful here:

newcommandfigspace{^^^^2007}

Answered by Davislor on December 4, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP