Emacs and Scrivener

I heard a lot of people talking about Scrivener so I downloaded a trial copy (on Windows, there’s no Linux version, sadly…) and gave it a try.

It seems like a suitable tool for writers to use, it comes with a nice tutorial, and I’d recommend that anyone give the free 30 day trial a go, (this despite the fact the company behind it are called Literature and Latte).

I still prefer Emacs, though. Most of the features that Scrivener offers are easily replicable in Emacs. If you’ve been following my Emacs Writing Tips you’ll have been doing something similar anyway.

One thing I did like about Scrivener was the corkboard, a place where you could pin synopsis cards and move them around whilst you’re putting your ideas in order. This is a really good idea, and one thing that Emacs can’t replicate so well.

Here’s a work around

The idea is quite straightforward

1. Write your synopsis in headings
2. Move them up and down using M-<up> and M-<down>

It’s not as nice as the corkboard, but if you want a graphical interface, you’re using the wrong program.

It’s also worth mentioning org-tree-to-indirect-buffer, here. This allows you to replicate the document map in Microsoft word. It’s easiest to try this out for yourself

1. Open an org file in Emacs
2. Navigate to the subtree you want to edit
3. C-x 3 to split the window vertically (org-tree-to-indirect-buffer defaults to a vertical split)
4. C-c C-x b to open the narrowed subtree in the other frame

It all sounds rather complicated, but it’s really quite straightforward, and very useful…

Moodle and LaTeX

Moodle and \LaTeX

\LaTeX is a high-quality typesetting system; it includes features designed for the production of technical and scientific documentation. \LaTeX is the de facto standard for the communication and publication of scientific documents. \LaTeX is available as free software.
Moodle comes with the ability to handle \LaTeX. Check that your administrator has enabled it.
To include \LaTeX code in Moodle, use the following:
$$Your-latex-Code$$
So $$A=\pi r^2$$
becomes
A=\pi r^2

Symbols

If you type $$\alpha, \beta, \gamma$$ into Moodle you get \alpha, \beta, \gamma.
Knowing this you can write formulas such as $$A = \pi r^2$$
Knowing this you can write formulas such as A = \pi r^2
Note the difference between $$\sigma and \Sigma$$.
Note the difference between \sigma and \Sigma
You can guess the names of most of the symbols you need...$$\pm,\le, \neq, \ge$$
You can guess the names of most of the symbols you need… \pm, \le, \neq, \ge
There are lots of other symbols: $$\forall , \leftarrow, \Rightarrow, \infty$$, $$\cos (2\theta) = \cos^2 \theta - \sin^2 \theta$$
There are lots of other symbols: \forall , \leftarrow, \Rightarrow, \infty, \cos (2\theta) = \cos^2 \theta - \sin^2 \theta
Find more symbols here: http://www.artofproblemsolving.com/Wiki/index.php/LaTeX:Symbols

Functions, Fractions and Derivatives

You’ve already seen how to write superscripts.  Use the underscore for subscripts: n_ij
Copper Sulphate:  $$CuSO_4$$ : CuSO_4
Use braces { } for clarity:
Fibonacci Sequence:  $$F_n = F_{n-1} +F_{n-2}$$ :  F_n = F_{n-1} +F_{n-2}
Here are some fractions
$$\frac{x+y}{y-z}$$ :
\frac{x+y}{y-z}
$$\frac{\frac{1}{x}+\frac{1}{y}}{y-z}$$
\frac{\frac{1}{x}+\frac{1}{y}}{y-z}:
You can probably guess what \sqrt does, so that gives us
$$x = \frac{-b \pm \sqrt{b^2 – 4ac }}{2a}$$
x = \frac{-b \pm \sqrt{b^2 - 4ac }}{2a}
Which you can copy and paste when you need it.
Here’s how to do derivatives
$$y = x^2$$
y = x^2
$$\frac{dy}{dx}=2x$$
\frac{dy}{dx}=2x

Brackets

Note the use of \left and \right to size the brackets correctly
$$\frac{x+y}{y-z}$$
\frac{x+y}{y-z}
$$(\frac{x+y}{y-z})$$
(\frac{x+y}{y-z})
$$\left(\frac{x+y}{y-z}\right)$$
\left(\frac{x+y}{y-z}\right)

Calc Mode 2: Two’s Complement

Emacs provides a method for working out numbers in two’s complement form

M-x calc        Turn on calc mode
O d2            Turns on two's complement binary
b w 8           Sets the word length to 8 bits
d z             Displays leading zeroes

Now you can enter numbers. Don’t forget to use underscore for negative numbers

_3 (-3) gives 2##11111101

You can press y to yank the result back into the last buffer.

Quick Conversions

if you want to quickly convert between binary, denary and hex you can use quick calc mode:
To convert 17 from Denary to Binary

C-x * q         Enter quick calc mode
17              Hit enter
Result: 17 =>  17  (16#11, 8#21, 2#10001, " ")

To convert 1100 from Binary to Denary (or Hex)

C-x * q         Enter quick calc mode
2#1100          Enter in minibuffer.  Use 16# to enter a Hex number
Result: 12 =>  12  (16#C, 8#14, 2#1100, " ")

See Also

Finding my Place

(This post appears as part of the document My Emacs Writing Setup: a guide to how I write novels and short stories using Emacs Org Mode)

It can be a nuisance finding where things are even when writing a short story. When writing a novel, it’s easy to get lost amongst the story, the notes, the character sketches…

Fortunately, Emacs and Org-Mode have a number of features to help you find your way around.

The most basic and most useful method, of course, is Emacs search. I’ve written more about this here.

Similar to search are

M-x             occur, and
C-c / r         org-occur

These highlight all occurrences of the search string. This can be handy, for example, when searching to see which chapters a character appears in.

Quite often I find myself jumping up and down a file, adding text here and there. You can use the standard mark-ring commands:

C-<SPC> C-<SPC>         Set the mark to the mark ring
C-u C-<SPC>             Move point to where the mark was

… but org-mode has two commands that respectively push your current position to the mark-ring and jump to the last position on the mark ring. For convenience, I’ve bound them to the f7 key as follows.

(global-set-key (kbd "<f7>") 'org-mark-ring-push)
(global-set-key (kbd "C-<f7>") 'org-mark-ring-goto)

Now, I simply hit f7 to remember my position before heading off to edit elsewhere in the file, then hit C-f7 to return to where I started.

Bookmarks

I’m assuming you already know how to use bookmarks, but if you don’t, here’s a link to the Emacs Wiki for a crash course.
As I write on multiple machines, I keep my bookmarks file on Dropbox so that I have a consistent set of bookmarks wherever I happen to be working. I’ve added the following command to my .emacs file to let Emacs know where my bookmarks are.

(setq bookmark-default-file "~/Dropbox/common/emacs/bookmarks.bmk" bookmark-save-flag 1)

I’ve got into the habit of having a bookmark named here. I try to set this mark when I finish working. When I start work, I simply jump to here.

Calc Mode 0: Basics

Having seen it written on a blackboard in the Simpsons, I wanted to check if

398712 + 436512 = 447212

thus disproving Fermat’s Last Theorem. My pocket calculator confirmed the expression to be true, but those are big numbers, and the calculator lacks precision.

This seemed like a perfect opportunity to use calc mode.

M-x calc        to enter calc mode

First enter the left hand side

3987
<Enter>
12 ^
4365
<Enter>
12 ^

Both numbers are now there in the stack:

--- Emacs Calculator Mode ---
2:  16134474609751291283496491970515151715346481
1:  47842181739947321332739738982639336181640625

Hit + to add them together. Now to enter the right hand side.

4472
<Enter>
12 ^

Now the left and right sides of the original expression are there in the stack, and you can see that whilst they may be sufficiently equal to fool a pocket calculator, they can’t fool Emacs.

--- Emacs Calculator Mode ---
2:  63976656349698612616236230953154487896987106
1:  63976656348486725806862358322168575784124416

Hit  to see the difference:

1211886809373872630985912112862690

d g to group the number, then hit y to yank the number back into the current buffer.

1,211,886,809,373,872,630,985,912,112,862,690

… as I just did there.

See Also

Calc Mode 1: Binary Numbers

– Loop Solutions

2 Loop Solutions

 1: def oneToTen():
 2:         for i in range(1,11):
 3:                 print(i)
 4: 
 5: def oneToTenAcross():
 6:       print("|", end="")
 7:         for i in range(1,11):
 8:                 print(i,"|",end="")
 9: 
10: def oddNumbers():
11:         for i in range(1,21,2):
12:                 print(i)
13: 
14: def squares():
15:         for i in range(1,11):
16:                 print(i*i)
17: 
18: def random4():
19:         for i in range(1,5):
20:                 print(random.randint(1,10))
21: 
22: def even(n):
23:         for i in range(1,n+1):
24: 
25: 
26: def powers(n):
27:         for i in range(1,n+1):
28:                 print(2**i)
29: 
30: def triangle():
31:         for i in range(1,6):
32:                 for j in range(1,i+1):
33:                         print("*", end = "")
34:                 print()
35: 
36: def areWeThereYet():
37:         s = "no"
38:         while (s != "yes"):
39:                 s = input("Are we there yet?")
40:         print ("Good!")
41: 
42: 
43: def tableSquare():
44:         for i in range(1,5):
45:                 for j in range (1, 5):
46:                         print("|",i*j, "\t", end = "")
47:                 print("|")
48: 
49: def tableSquares(n):
50:         for i in range(1,n+1):
51:                 for j in range (1, n+1):
52:                         print("|",i*j, "\t", end = "")
53:                 print("|")

2: Loops

2.1 One to 10

Write a function that prints the numbers 1 to 10

2.1.1 Example

 1: oneToTen()
 2:  *** Output ***
 3: 1
 4: 2
 5: 3
 6: 4
 7: 5
 8: 6
 9: 7
10: 8
11: 9
12: 10

2.2 One to 10 across

In Python 3, you can use the end=”” parameter in print to prevent a newline at the end of a print(). For example, print(“frog”,end=””) will print frog without moving to the next line. Use that to write a function that prints the numbers 1 to 10 across the screen as shown

2.2.1 Example

1: oneToTenAcross()
2:  *** Output ***
3: |1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |

2.3 Odd Numbers

Write a function that prints the positive odd numbers less than 20

2.3.1 Example

 1: oddNumbers()
 2:  *** Output ***
 3: 1
 4: 3
 5: 5
 6: 7
 7: 9
 8: 11
 9: 13
10: 15
11: 17
12: 19

2.4 Square Numbers

Write a function that prints the square numbers up to 100

2.4.1 Example

 1: squares()
 2:  *** Output ***
 3: 1
 4: 4
 5: 9
 6: 16
 7: 25
 8: 36
 9: 49
10: 64
11: 81
12: 100

2.5 Random Numbers

Write a for loop to print out four random integers between 1 and 10

2.5.1 Example

1: random4()
2:  *** Output ***
3: 3
4: 5
5: 2
6: 8

2.6 Even Numbers < n

Write a function to print out the positive even numbers less than n

2.6.1 Example

 1: even(20)
 2:  *** Output ***
 3: 2
 4: 4
 5: 6
 6: 8
 7: 10
 8: 12
 9: 14
10: 16
11: 18

2.7 Powers of 2

Write a function to print out the powers of 2 from 21 up to 2n

2.7.1 Example

 1: powers(8)
 2:  *** Output ***
 3: 2
 4: 4
 5: 8
 6: 16
 7: 32
 8: 64
 9: 128
10: 256

2.8 Are we there yet?

Write a program that outputs “Are we there yet?” and then waits for input. If the input is “Yes” the program outputs “Good!” and exits, otherwise the program loops.

2.8.1 Example

1: "Are we there yet?"
2: No
3: "Are we there yet?"
4: Spoons
5: "Are we there yet?"
6: Yes
7: Good!

2.9 Triangle

Write a function that uses nested loops to produce the following pattern.

1: triangle()
2:  *** Output ***
3: *
4: **
5: ***
6: ****
7: *****

2.10 Table Square

Write a function that prints out a 4 x 4 table square

2.10.1 Example

1: tableSquare()
2:  *** Output ***
3: A 4 x 4 table square
4: | 1 | 2 |  3 |  4 |
5: | 1 | 2 |  3 |  4 |
6: | 2 | 4 |  6 |  8 |
7: | 3 | 6 |  9 | 12 |
8: | 4 | 8 | 12 | 16 |

2.11 Table Squares

Extend your answer to the last question produce a function that will print out a n x n table square

2.11.1 Example

1: tableSquares(6)
2:  *** Output ***
3: A 6 x 6 table square
4: | 1 |  2 |  3 |  4 |  5 |  6 |
5: | 2 |  4 |  6 |  8 | 10 | 12 |
6: | 3 |  6 |  9 | 12 | 15 | 18 |
7: | 4 |  8 | 12 | 16 | 20 | 24 |
8: | 5 | 10 | 15 | 20 | 25 | 30 |
9: | 6 | 12 | 18 | 24 | 30 | 36 |

1: How to Answer these Questions

Write functions to solve all of the questions. Here are two example questions and their solutions. Notice that the first function prints a value, the second function returns a value.

1 Hello <Name>

Write a function that accepts a name as a parameter and prints out “Hello ” <name>

1.1 Example

hello("Kim")
 *** Output ***
Hello Kim

2 Average of two numbers

Write a function that accepts two numbers and returns the average of the two numbers.

2.1 Example

print(average(3,4));
 *** Output ***
3.5

3 Solutions

1: def hello(s):
2:     print("Hello ",s)
3: 
4: def average(i,j):
5:     return (i+j)/2
6: 
7: hello("Kim")
8: print(average(3,5))

What’s Wrong with the Raspberry Pi?

Nothing, as such. The Raspberry Pi is a great piece of equipment, a fully working computer available for only a few pounds.

The problem with the Raspberry Pi is its suggested use as a teaching tool. The very existence of the Raspberry Pi is an example of one of the most common fallacies in teaching – “I learned something this way, and it worked, so therefore everyone else must learn things this way as well.”

Like most people my age I learned to program in BASIC on very simple machines: Sinclair ZX81s and VIC 20s. What worked for me when learning programming was a simple machine that I could understand fully. But I was interested in programming, and I was willing to put up with the difficulties of a text based interface. Most importantly, those machines were the only computers I had ever seen.

Things are very different today. Children have grown up in a world of computers. They have a very different understanding of what a computer is and what it can do than we did. Getting them to program in Python on a stripped down box merely suggests that such things aren’t possible on a regular PC. It’s far better to get them to download an SDK onto a machine they’re familiar with than to get them started in a completely strange environment.

One last thing: Raspberry Pis aren’t even cheap. The machine itself may be, but you still need a monitor, a keyboard and mouse, and most importantly in a school, somewhere to set them up. And unless you intend to only set them up before each lesson, they are going to sit there doing nothing for most of the time. Of course, you could always put them on a trolley. Putting computer equipment on trolleys has been a great feature of British Education over the past twenty years. The school computers in this country must have covered more miles to less effect than the IT equipment of any other nation on Earth.

If you’re looking for something simple and cheap, the best machine you can get your hands on is on old PC installed with Lubuntu. Many British families probably have an old PC at home that they could use already. What better way to learn to learn about computers?

Regexp Exercises

You might want read the post on Regexp Builder before attempting these…

Here’s an example of a British Postcode: OL1 3SQ. It has the format 2 letters and a digit, space, 1 digit and two letters. The following emacs regex finds this type of postcode:

“[A-Z]\{2\}[0-9] [0-9][A-Z]\{2\}”

  1. Write a regex to find postcodes of the type W1 1AA
  2. Write a regex to find postcodes of the type RM12 4JJ
  3. Write one regex that finds all three types of postcode: OL1 3SQ, W1 1AA and RM12 4JJ
  4. Write a regex that finds simple email addresses of the form [email protected]
  5. Now extend the regex to find email addresses of the form [email protected]
  6. Now write a general email regex that will find all properly formed email addresses
  7. Google the form of an ISBN (There are two standards). Write a regex to find ISBNs
  8. Write a regex that can find unnecessary white space in a line of text

This      line     has              unnecessary      white space    .
|              So does this line   |
This line does not.

Finally…

“He said ‘Have you got the time?’” “Why didn’t he look at his watch?”
Write a regex that can find the left single quotes: ‘and right double quotes: ” in the above conversation.