# How do I typeset morse code with LaTeX

I am writing an article regarding the history of the Morse code and I am having difficulties printing the dots and dashed (they get combined when the document is printed).

Is it also possible to write a macro that can map the Morse code to words and letterrs? For example the letter a is ".-" etc.

-

Here is a slightly different solution than the others posted.

First we define two lists to hold the morse code. One for letters and another one for numbers

\def\morselist{.-,-...,-.-.,-..,.,..-.,%
--.,....,..,.---,-.-,.-..,%
--,-.,---,.--.,--.-,.-.,%
...,-,..-,...-,.--,-..-,-.--,--..}


This list is then used to create macros for every letter and number. Once this is achieved one can parse any word and print the corresponding Morse symbols.

\documentclass{article}
\usepackage{soul}
\newcounter{ct}
\begin{document}
\makeatletter

% the alphabet list
\def\morselist{.-,-...,-.-.,-..,.,..-.,%
--.,....,..,.---,-.-,.-..,%
--,-.,---,.--.,--.-,.-.,%
...,-,..-,...-,.--,-..-,-.--,--..}

% get the numbers 48-57
\def\morsenumbers{.----,..---,...--,....-,
.....,-....,--...,---..,----.,-----}

% letters
\setcounter{ct}{97}
\@for \i:=\morselist\do{%
\texttt{\char\thect =\i}\par
\def\MM{\i}%
\expandafter\xdef\csname\thect @\endcsname{\MM}%
\stepcounter{ct}%
}
% numbers
\setcounter{ct}{48}
\@for \i:=\morsenumbers \do{%
\texttt{\char\thect =\i}\par
\def\MM{\i}%
\expandafter\xdef\csname\thect @\endcsname{\MM}%
\stepcounter{ct}%
}

\def\printMorse#1{%
\texttt{\@nameuse{\number#1@}}
}

\def\getMorseWord@#1#2\relax{%
\ifx\relax#2\relax
#1=\printMorse{#1}
\else
#1=\printMorse{#1}%\par
\getMorseWord@#2\relax
\fi
}

\def\getMorseWord#1{%
\getMorseWord@ #1\relax
}

% type in the word you want printed in
% morse code here.
\getMorseWord{saltypen sos}
\makeatother
\end{document}

-
What is the upside of writing this yourself vs the morse package below? – Canageek Sep 24 '11 at 18:06
@Canageek The upside for me was and is that I retain my skills with TeX/LaTeX programming:) If you learn to program you can do amazing things. – Yiannis Lazarides Sep 25 '11 at 10:12

There's the morse package; it's a little old, but it seems to work fine, although I wouldn't know for sure since I don't know anything about the Morse code (except that it exists); a little example:

\documentclass[12pt]{article}
\usepackage{morse}

\newcommand\LatMor[1]{%
the letter #1 is {\morse #1}}

\begin{document}

\LatMor{a}\LatMor{b}\LatMor{x}\LatMor{y}\LatMor{z}

{\morse M o r s e T e x t}

{\Large\morse  M o r s e T e x t}

\end{document}


Probably you will have to install the package manually, but it seems easy; I just did the test copying all the files in my working directory and everything was OK.

EDIT: I updated the answer with the definition of the \LatMor command.

-
Letters seem to match with what Wikipedia says, punctuation doesn't. – Caramdir May 26 '11 at 2:28

More seriously, if you have a Morse code font in ttf or otf format, you can use fontspec to you that font:

% compile with lualatex or xelatex
\documentclass{article}
\usepackage{fontspec}

\newfontface\morse{Morse Code} % replace with the actual name of the font

\begin{document}
{\morse Some Text}
\end{document}

-

Another solution, using the listings package and the literate key :

\documentclass{standalone}

\newcommand{\dt}{\kern-0.5pt\raisebox{0.4ex}{.}}

\usepackage{listings}
\lstset{%
literate={a}{\dt-}3{b}{-\dt\dt\dt}3{c}{-\dt-\dt}3{z}{-{}-\dt\dt}3}

\begin{document}

\lstinline{abc}

\end{document}


The result is

-

ConTeXt has a lua based module for morse code. It should be straight forward to translate it to LaTeX.

-

When I needed to typeset a little Morse code for a book, I just used \rule and experimented with the width and length to make a nice dot and dash. I defined macros for those and then I used them to typeset what I wanted. Unfortunately $\rule{2pt}{2pt}$` doesn't work here.

-
I'm not sure that I understand... Does this work? If so please provide a demonstration. – qubyte Feb 19 '12 at 13:43
You can use $\rule{2pt}{2pt}$ to typeset a 2pt square of ink. Treat that like a "dot". Similarly, you can use the \rule command to typeset "dashes" that are wider than they are high. – Carl Mummert Feb 19 '12 at 20:45