Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ cd [Path of source code folder]
bundle exec jekyll serve
```
6. Navigate to http://localhost:4000 in a browser.
7. Add your own Markdown posts inside `_posts`.
7. Add your own Markdown posts inside the `_posts` folder.
8. Profit!
52 changes: 26 additions & 26 deletions _posts/2025-02-20-groff-apa.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ permalink: /groff-apa

This guide explains why and how I used groff with the ms and refer macros to write my college essay in Neovim.

# Table of Contents <a name=tableofcontents></a>
# 1. Table of Contents <a name=tableofcontents></a>

1. [Table of Contents](/groff-apa#tableofcontents)

Expand Down Expand Up @@ -45,7 +45,7 @@ This guide explains why and how I used groff with the ms and refer macros to wri

11. [Conclusion](/groff-apa#conclusion)

# Preface <a name=preface></a>
# 2. Preface <a name=preface></a>

Many colleges give students a mandatory class where they learn how to cite and do referencing in essays.

Expand All @@ -59,7 +59,7 @@ I made a [Github repository](https://github.com/de-soot/groff-apa) as a companio

A fun fact about this blog post: this is actually my second time writing this whole thing because I frustratingly lost the first one in the dumbest way possible. I accidentally deleted the folder containing it because I forgot that I put it in the folder. Unfortunately, it was already gone by the time I realised due to the auto-emptying routine of my Trash folder scheduled to run once every hour.

# Introduction <a name=introduction></a>
# 3. Introduction <a name=introduction></a>

The [American Psychological Association](https://apa.org) (APA) 7th Edition [style guide](https://apastyle.apa.org) is the most popular for formatting references and citations in college-level essays (at least that was what my lecturer said when they gave out the writing assignment).

Expand All @@ -69,11 +69,11 @@ The [ms](https://www.gnu.org/software/groff/manual/groff.html.node/ms.html) ("ma

However, groff by itself cannot do citations and referencing out-of-the-box. Fortunately, it does come with a preprocessor called refer that does the citing and referencing for it. Although the defaults for refer do not conform to APA guidelines, it is not too complex to change those defaults to match any style guide as long as you know where to find the right things to change.

# Prerequisites <a name=prerequisites></a>
# 4. Prerequisites <a name=prerequisites></a>

This guide assumes you are using a Unix-based system (e.g. GNU/Linux, *BSD, macOS, etc.) and already know how to write and compile basic `groff ms` documents to Portable Document Format (PDF). If not, do not worry. There are many resources online for getting started, such as Luke Smith's videos on [groff](https://videos.lukesmith.xyz/w/eDqgKby1W4sHQGZgqZbMLe) and [refer](https://videos.lukesmith.xyz/w/5ANbTYv7cgF69FhpAkVBwi). For more detail, see GNU's official [manual](https://www.gnu.org/software/groff/manual/groff.html) or `troff`'s [HOWTO](https://troff.org/TheGroffFriendsHowto.pdf).

# Cover Page <a name=coverpage></a>
# 5. Cover Page <a name=coverpage></a>

My assignment did not require me to include a cover page, but yours might. By default, groff puts cover page information on the same page as the essay. Luckily, the ms macros contain a convenient way to change this. Just put [`.RP no`](https://linux.die.net/man/7/groff_ms) as the first line of your document (`.RP` stands for "report"; the `no` argument tells it to not repeat the cover page information on the next page):

Expand All @@ -83,7 +83,7 @@ My assignment did not require me to include a cover page, but yours might. By de

This is also needed for the next section on page numbering.

# Page Numbering <a name=pagenumbering></a>
# 6. Page Numbering <a name=pagenumbering></a>

The first page does not have any headers (including page numbering) because groff expects it to include cover page information (i.e. the title of the paper; optionally author(s), institution(s), date, and abstract).

Expand All @@ -110,23 +110,23 @@ The `.RP no` macro is needed for this, but it produces a cover page. Use PDF-edi

which works because even though the title (`.TL`) is required, it stops at the abstract (`.AB`, which ends at `.AE`; the `no` argument suppresses the "Abstract" heading).

## Suppress Page Headers <a name=suppressheader></a>
## 6.1 Suppress Page Headers <a name=suppressheader></a>

If instead you want to **not** have any page headers (including page numbering) for reasons such as plagerism report checkers potentially producing errors, disabling automatic page numbering is as easy as defining the central header to be empty:

```
.ds CH
```

# Font <a name=font></a>
# 7. Font <a name=font></a>

Many lecturers demand assignments to be submitted in double-spaced Times New Roman in 12 point size. This section covers how to change the font family, size, style, and line spacing.

## Font Family <a name=fontfamily></a>
## 7.1 Font Family <a name=fontfamily></a>

By default, groff already outputs text in your [device's default serif font family](https://www.gnu.org/software/groff/manual/groff.html.node/Using-Fonts.html) (typically Times New Roman), but sometimes (very rarely, in my experience) lecturers prefer sans-serif or even monospace in some cases.

### Default Fonts <a name=defaultfonts></a>
### 7.1.1 Default Fonts <a name=defaultfonts></a>

Changing to another one of the default fonts in groff is simple.

Expand All @@ -150,11 +150,11 @@ and (back to) serif font family:

There are many other default fonts in groff listed in [this post](https://technicallywewrite.com/2024/04/12/changefonts).

### Custom Fonts <a name=customfonts></a>
### 7.1.2 Custom Fonts <a name=customfonts></a>

Installing custom fonts for groff is a little bit trickier; I have not been able to get it to work on my device yet. I did find a [guide that seemed promising](https://www.port.de/cgi-bin/groff/AddingFonts) though. Maybe I will revisit this if I get it working in the future.

## Font Size <a name=fontsize></a>
## 7.2 Font Size <a name=fontsize></a>

The default font size in groff is a little bit less than 12pt ([10pt](https://www.gnu.org/software/groff/manual/groff.html.node/Manipulating-Type-Size-and-Vertical-Spacing.html), to be exact), but it is very easy to change. Simply add this line to your plaintext document to change the font size to 12pt:

Expand All @@ -164,15 +164,15 @@ The default font size in groff is a little bit less than 12pt ([10pt](https://ww

As for what `PS` stands for, it probably means something like "Point Size"; `.nr` stands for "Number Registers".

## Line Spacing <a name=linespacing></a>
## 7.3 Line Spacing <a name=linespacing></a>

All parts of an APA-styled paper [should be double-spaced](https://apastyle.apa.org/style-grammar-guidelines/paper-format/line-spacing). By default, groff uses [120% of font size](https://www.gnu.org/software/groff/manual/groff.html.node/Manipulating-Type-Size-and-Vertical-Spacing.html) as vertical spacing. Fortunately, changing this to double-spacing is just as simple as changing the font size: just set the vertical spacing (`.nr VS`) to double your font size (`.nr PS`).

```
.nr VS 24
```

## Font Style <a name=fontstyle></a>
## 7.4 Font Style <a name=fontstyle></a>

Here is how to make your text **bold**,

Expand All @@ -192,13 +192,13 @@ And to set it back to normal:
.ft R \" Stands for Regular or Roman
```

# In-text Citation <a name=intextcitation></a>
# 8. In-text Citation <a name=intextcitation></a>

Now that everything is set up, we can start to get into the citations.

refer uses numbered footnotes by default, but APA wants the author(s) and date included in their in-text citations. This section will cover how to do exactly as APA says.

## Parenthetical Citation <a name=parentheticalcitation></a>
## 8.1 Parenthetical Citation <a name=parentheticalcitation></a>

After some research, I found a [blog post "no-tears" guide](https://preciouschicken.com/blog/posts/no-tears-references-groff), 2 [Github](https://github.com/Koshkov/groff-paper-template) [repositories](https://github.com/skurtulmus/refer-styles), and a post from the [GNU mailing list archive](https://lists.gnu.org) (which I cannot seem to find anymore). I extracted the refer block (denoted by `.R1` and `.R2`) from all those sources and combined them to make a frankenstein that works perfectly for most APA parenthetical citations:

Expand All @@ -215,17 +215,17 @@ join-authors " & " ", " " & " # Change default "and" to ampersand (&) when joini
.R2 \" Refer block end
```

## Narrative Citation <a name=narrativecitation></a>
## 8.2 Narrative Citation <a name=narrativecitation></a>

My lecturer luckily did not require me to use narrative citations, but yours might. If you do have to use narrative citation, unfortunately, refer does not have any support for narrative citations being alongside parenthetical citations. While it is possible to repurpose the citation formatting part of the refer block to change it from parenthetical to narrative, it would still not be possible to have both narrative and parenthetical citation on the same page.

The only practical solution I can think of for this problem is to keep the refer block as-is and write the narrative citations manually. This will require you to have a separate document for producing the referencing for the narrative citations, because refer will not make entries for them in the reference list as it will not recognise the narrative citations that you manually typed in, treating them like ordinary text. This also means that you need to generate 2 separate output PDFs and extract the correct reference list from the one without narrative citations to merge (using PDF-editing software) with the other one that does.

# Reference List <a name=referencelist></a>
# 9. Reference List <a name=referencelist></a>

This section is where we start to dive into refer's code (usually located inside either `/usr/share/groff/current/tmac` or `/usr/local/share/groff/current/tmac`). Rest assured, the changes that will be made will not be difficult as long as you know 3 things: what to change, what to change them to, and where to find them (all 3 being what I had to figure out on my own by reading and tinkering with the source code).

## Formatting the Heading <a name=formattingtheheading></a>
## 9.1 Formatting the Heading <a name=formattingtheheading></a>

The "References" heading is left-aligned by default, whereas [APA wants it centered](https://apastyle.apa.org/style-grammar-guidelines/paper-format/reference-list). Some extra changes not specified by APA my lecturer wanted were to make the Heading a little bigger and add an extra line of space below it.

Expand Down Expand Up @@ -259,7 +259,7 @@ Below is a list explaining each change made:
- (Optional) `.nr PS 14` above it increases the font size to 14pt, just remember to reset it back to normal (12pt) with `.nr PS 12` after.
- (Optional) `.sp 2` adds two extra lines of space below the heading.

## Space Between Entries <a name=spacebetweenentries></a>
## 9.2 Space Between Entries <a name=spacebetweenentries></a>

There is no space between reference entries by default. This is already conforming to APA, but my lecturer wanted extra padding between the entries (even if that meant [breaking APA guidelines](https://apastyle.apa.org/style-grammar-guidelines/paper-format/reference-list)) and maybe yours might too.

Expand All @@ -280,7 +280,7 @@ Inside `refer-ms.tmac` on lines 44--45, you can find this:

{%comment%}

### For Specific Entries <a name=forspecificentries></a>
### 9.2.1 For Specific Entries <a name=forspecificentries></a>

For reference lists that span more than one page, some entries (especially large ones) may be split between 2 pages. While it would be easy to press the enter key a couple times in a WYSIWYG word processor, you should know by now that things are not going to be that easy in groff and refer.

Expand All @@ -300,7 +300,7 @@ To fix an entry being split between pages, first identify the entry above it and

{%endcomment%}

## Field Ordering <a name=fieldordering></a>
## 9.3 Field Ordering <a name=fieldordering></a>

The way refer orders the fields of each reference entry is hard-coded and different from what APA desires. More specifically, the date field is placed after the title field instead of being in between it and the author(s) field.

Expand Down Expand Up @@ -336,7 +336,7 @@ Rearranging `D` (date) into the correct order (right after `A` (author(s)) and b
.ds ref*spec!4 Q A D T R G P I C O
```

## Punctuation <a name=punctuation></a>
## 9.4 Punctuation <a name=punctuation></a>

Switching the order of the fields messes up the hard-coded punctuation order, but there are also some defaults that do not follow APA guidelines.

Expand Down Expand Up @@ -421,7 +421,7 @@ The list below details the changes:
- All `\fP` in `arg 5` are moved to `arg 4` to make sure the punctuations after each field are not in bold or italics.
- When numbering pages, APA does not denote with `p.` for single pages or `pp.` for page ranges, so they were removed in their respective rows.

### Period Suppression <a name=periodsuppression></a>
### 9.4.1 Period Suppression <a name=periodsuppression></a>

By default, refer appends a period at the end of each entry. This means that a period is added after the last field that contains a DOI or URL, which APA has [explicitly stated to not do](https://apastyle.apa.org/instructional-aids/reference-guide.pdf).

Expand All @@ -443,7 +443,7 @@ Now just remove the period (`.`) on the last line (213):
. as ref*string
```

# Exporting as PDF <a name=exportingaspdf></a>
# 10. Exporting as PDF <a name=exportingaspdf></a>

The plaintext document can be compiled with either `groff`,

Expand All @@ -466,7 +466,7 @@ Below is a list explaining what each of the optional arguments mean:
- `-R` selects refer macros.
- (Optional) `-dpaper=a4` and `-P-pa4` exports the paper to the international-standard A4 size instead of the default US letter.

# Conclusion <a name=conclusion></a>
# 11. Conclusion <a name=conclusion></a>

If you found this helpful in any way or have any questions, feel free to leave a comment below. Also read my [previous guide](/justify-text) on how I justify and hyphenate the text on this website.

Expand Down
24 changes: 22 additions & 2 deletions _posts/2025-12-08-2s-complement.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ If we flip all the bits of `x` using the bitwise NOT operator (`~`), we get a ne
We will call this new number `~x`, where `x + ~x = 2^n - 1`, since 2^n - 1 has all `n` number of bits set to `1`.

Rearranging the above equation for `x`, we get `x = (2^n - 1) - ~x`.
It is then easy to find that `-x = -(2^n - 1 - ~x) = -2^n + 1 + ~x`.
Now see the picture of where "flip the bits and add one" comes from.
It is then easy to find that

`-x = -(2^n - 1 - ~x) = -2^n + 1 + ~x`.

Now start to see the picture of where "flip the bits and add one" comes from.

But we cannot just flip bits and add 1 to get `-x` with ordinary bits, that would just be `~x + 1`.
We are still missing the `-2^n` in the equation, but how would we get that?
Expand All @@ -36,3 +39,20 @@ When we flip the bits of `x`, this new bit we added will also get flipped along
Putting all the pieces together, we get that for a number `x` represented by `n` bits (with an extra bit having a place value of `-2^n`) where `x = (2^n - 1) - ~x`, you would need to flip its bits and add one to get `-2^n + ~x + 1 = -x`.

Hopefully after reading this, you will have gained a better understanding of using 2's complement for converting binary numbers to negative.

<script src="https://giscus.app/client.js"
data-repo="de-soot/de-soot.github.io"
data-repo-id="R_kgDOK6_5tA"
data-category="Announcements"
data-category-id="DIC_kwDOK6_5tM4CflCT"
data-mapping="title"
data-strict="0"
data-reactions-enabled="1"
data-emit-metadata="0"
data-input-position="top"
data-theme="preferred_color_scheme"
data-lang="en"
data-loading="lazy"
crossorigin="anonymous"
async>
</script>
2 changes: 1 addition & 1 deletion _site/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ cd [Path of source code folder]
bundle exec jekyll serve
```
6. Navigate to http://localhost:4000 in a browser.
7. Add your own Markdown posts inside `_posts`.
7. Add your own Markdown posts inside the `_posts` folder.
8. Profit!
Loading