You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2025-02-20-groff-apa.md
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ permalink: /groff-apa
8
8
9
9
This guide explains why and how I used groff with the ms and refer macros to write my college essay in Neovim.
10
10
11
-
# Table of Contents <aname=tableofcontents></a>
11
+
# 1. Table of Contents <aname=tableofcontents></a>
12
12
13
13
1.[Table of Contents](/groff-apa#tableofcontents)
14
14
@@ -45,7 +45,7 @@ This guide explains why and how I used groff with the ms and refer macros to wri
45
45
46
46
11.[Conclusion](/groff-apa#conclusion)
47
47
48
-
# Preface <aname=preface></a>
48
+
# 2. Preface <aname=preface></a>
49
49
50
50
Many colleges give students a mandatory class where they learn how to cite and do referencing in essays.
51
51
@@ -59,7 +59,7 @@ I made a [Github repository](https://github.com/de-soot/groff-apa) as a companio
59
59
60
60
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.
61
61
62
-
# Introduction <aname=introduction></a>
62
+
# 3. Introduction <aname=introduction></a>
63
63
64
64
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).
65
65
@@ -69,11 +69,11 @@ The [ms](https://www.gnu.org/software/groff/manual/groff.html.node/ms.html) ("ma
69
69
70
70
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.
71
71
72
-
# Prerequisites <aname=prerequisites></a>
72
+
# 4. Prerequisites <aname=prerequisites></a>
73
73
74
74
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).
75
75
76
-
# Cover Page <aname=coverpage></a>
76
+
# 5. Cover Page <aname=coverpage></a>
77
77
78
78
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):
79
79
@@ -83,7 +83,7 @@ My assignment did not require me to include a cover page, but yours might. By de
83
83
84
84
This is also needed for the next section on page numbering.
85
85
86
-
# Page Numbering <aname=pagenumbering></a>
86
+
# 6. Page Numbering <aname=pagenumbering></a>
87
87
88
88
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).
89
89
@@ -110,23 +110,23 @@ The `.RP no` macro is needed for this, but it produces a cover page. Use PDF-edi
110
110
111
111
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).
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:
116
116
117
117
```
118
118
.ds CH
119
119
```
120
120
121
-
# Font <aname=font></a>
121
+
# 7. Font <aname=font></a>
122
122
123
123
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.
124
124
125
-
## Font Family <aname=fontfamily></a>
125
+
## 7.1 Font Family <aname=fontfamily></a>
126
126
127
127
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.
128
128
129
-
### Default Fonts <aname=defaultfonts></a>
129
+
### 7.1.1 Default Fonts <aname=defaultfonts></a>
130
130
131
131
Changing to another one of the default fonts in groff is simple.
132
132
@@ -150,11 +150,11 @@ and (back to) serif font family:
150
150
151
151
There are many other default fonts in groff listed in [this post](https://technicallywewrite.com/2024/04/12/changefonts).
152
152
153
-
### Custom Fonts <aname=customfonts></a>
153
+
### 7.1.2 Custom Fonts <aname=customfonts></a>
154
154
155
155
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.
156
156
157
-
## Font Size <aname=fontsize></a>
157
+
## 7.2 Font Size <aname=fontsize></a>
158
158
159
159
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:
160
160
@@ -164,15 +164,15 @@ The default font size in groff is a little bit less than 12pt ([10pt](https://ww
164
164
165
165
As for what `PS` stands for, it probably means something like "Point Size"; `.nr` stands for "Number Registers".
166
166
167
-
## Line Spacing <aname=linespacing></a>
167
+
## 7.3 Line Spacing <aname=linespacing></a>
168
168
169
169
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`).
170
170
171
171
```
172
172
.nr VS 24
173
173
```
174
174
175
-
## Font Style <aname=fontstyle></a>
175
+
## 7.4 Font Style <aname=fontstyle></a>
176
176
177
177
Here is how to make your text **bold**,
178
178
@@ -192,13 +192,13 @@ And to set it back to normal:
192
192
.ft R \" Stands for Regular or Roman
193
193
```
194
194
195
-
# In-text Citation <aname=intextcitation></a>
195
+
# 8. In-text Citation <aname=intextcitation></a>
196
196
197
197
Now that everything is set up, we can start to get into the citations.
198
198
199
199
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.
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:
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.
221
221
222
222
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.
223
223
224
-
# Reference List <aname=referencelist></a>
224
+
# 9. Reference List <aname=referencelist></a>
225
225
226
226
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).
227
227
228
-
## Formatting the Heading <aname=formattingtheheading></a>
228
+
## 9.1 Formatting the Heading <aname=formattingtheheading></a>
229
229
230
230
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.
231
231
@@ -259,7 +259,7 @@ Below is a list explaining each change made:
259
259
- (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.
260
260
- (Optional) `.sp 2` adds two extra lines of space below the heading.
261
261
262
-
## Space Between Entries <aname=spacebetweenentries></a>
262
+
## 9.2 Space Between Entries <aname=spacebetweenentries></a>
263
263
264
264
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.
265
265
@@ -280,7 +280,7 @@ Inside `refer-ms.tmac` on lines 44--45, you can find this:
280
280
281
281
{%comment%}
282
282
283
-
### For Specific Entries <aname=forspecificentries></a>
283
+
### 9.2.1 For Specific Entries <aname=forspecificentries></a>
284
284
285
285
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.
286
286
@@ -300,7 +300,7 @@ To fix an entry being split between pages, first identify the entry above it and
300
300
301
301
{%endcomment%}
302
302
303
-
## Field Ordering <aname=fieldordering></a>
303
+
## 9.3 Field Ordering <aname=fieldordering></a>
304
304
305
305
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.
306
306
@@ -336,7 +336,7 @@ Rearranging `D` (date) into the correct order (right after `A` (author(s)) and b
336
336
.ds ref*spec!4 Q A D T R G P I C O
337
337
```
338
338
339
-
## Punctuation <aname=punctuation></a>
339
+
## 9.4 Punctuation <aname=punctuation></a>
340
340
341
341
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.
342
342
@@ -421,7 +421,7 @@ The list below details the changes:
421
421
- All `\fP` in `arg 5` are moved to `arg 4` to make sure the punctuations after each field are not in bold or italics.
422
422
- 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.
423
423
424
-
### Period Suppression <aname=periodsuppression></a>
424
+
### 9.4.1 Period Suppression <aname=periodsuppression></a>
425
425
426
426
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).
427
427
@@ -443,7 +443,7 @@ Now just remove the period (`.`) on the last line (213):
443
443
. as ref*string
444
444
```
445
445
446
-
# Exporting as PDF <aname=exportingaspdf></a>
446
+
# 10. Exporting as PDF <aname=exportingaspdf></a>
447
447
448
448
The plaintext document can be compiled with either `groff`,
449
449
@@ -466,7 +466,7 @@ Below is a list explaining what each of the optional arguments mean:
466
466
-`-R` selects refer macros.
467
467
- (Optional) `-dpaper=a4` and `-P-pa4` exports the paper to the international-standard A4 size instead of the default US letter.
468
468
469
-
# Conclusion <aname=conclusion></a>
469
+
# 11. Conclusion <aname=conclusion></a>
470
470
471
471
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.
Copy file name to clipboardExpand all lines: _posts/2025-12-08-2s-complement.md
+22-2Lines changed: 22 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,8 +21,11 @@ If we flip all the bits of `x` using the bitwise NOT operator (`~`), we get a ne
21
21
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`.
22
22
23
23
Rearranging the above equation for `x`, we get `x = (2^n - 1) - ~x`.
24
-
It is then easy to find that `-x = -(2^n - 1 - ~x) = -2^n + 1 + ~x`.
25
-
Now see the picture of where "flip the bits and add one" comes from.
24
+
It is then easy to find that
25
+
26
+
`-x = -(2^n - 1 - ~x) = -2^n + 1 + ~x`.
27
+
28
+
Now start to see the picture of where "flip the bits and add one" comes from.
26
29
27
30
But we cannot just flip bits and add 1 to get `-x` with ordinary bits, that would just be `~x + 1`.
28
31
We are still missing the `-2^n` in the equation, but how would we get that?
@@ -36,3 +39,20 @@ When we flip the bits of `x`, this new bit we added will also get flipped along
36
39
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`.
37
40
38
41
Hopefully after reading this, you will have gained a better understanding of using 2's complement for converting binary numbers to negative.
0 commit comments