36

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.

8 Answers 8

20

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}
2
  • What is the upside of writing this yourself vs the morse package below?
    – Canageek
    Sep 24, 2011 at 18:06
  • 4
    @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.
    – yannisl
    Sep 25, 2011 at 10:12
17

Then answer


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}
17

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.

1
  • Letters seem to match with what Wikipedia says, punctuation doesn't.
    – Caramdir
    May 26, 2011 at 2:28
8

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

enter image description here

5

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

Here is an example usage:

\usemodule[morse]

\starttext
\Morse{This is some text in English that will be translated to Morse code}

\blank[big]

\MorseCode{—·—· ——— —· — · —··— —+—— —·— ·· ···—}
\stoptext

which gives

enter image description here

1
  • 404 - Object does not exist Apr 2, 2018 at 7:57
1

Here a solution based on TikZ. The idea is based loosly on my package TikZ-Timing. So far it only includes A-Z and 0-9 letters. You can change the length by using the x=<unit> key.

\documentclass{article}

\usepackage{tikz}
\makeatletter

\def\@morse@char#1{\expandafter\def\csname @morse@char@#1\endcsname}

\def\tikzmorse@wordsep{++(4, 0)}
\def\tikzmorse@charsep{++(2, 0)}
\def\tikzmorse@Dit{ -- ++(1,0) ++(1,0) }
\def\tikzmorse@Dah{ -- ++(3,0) ++(1,0) }

\@morse@char{A}{\tikzmorse@Dit\tikzmorse@Dah}
\@morse@char{B}{\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{C}{\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dit}
\@morse@char{D}{\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{E}{\tikzmorse@Dit}
\@morse@char{F}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dit}
\@morse@char{G}{\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dit}
\@morse@char{H}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{I}{\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{J}{\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah}
\@morse@char{K}{\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dah}
\@morse@char{L}{\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{M}{\tikzmorse@Dah\tikzmorse@Dah}
\@morse@char{N}{\tikzmorse@Dah\tikzmorse@Dit}
\@morse@char{O}{\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah}
\@morse@char{P}{\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dit}
\@morse@char{Q}{\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dah}
\@morse@char{R}{\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dit}
\@morse@char{S}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{T}{\tikzmorse@Dah}
\@morse@char{U}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dah}
\@morse@char{V}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dah}
\@morse@char{W}{\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dah}
\@morse@char{X}{\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dah}
\@morse@char{Y}{\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dah}
\@morse@char{Z}{\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{1}{\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah}
\@morse@char{2}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah}
\@morse@char{3}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dah\tikzmorse@Dah}
\@morse@char{4}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dah}
\@morse@char{5}{\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{6}{\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{7}{\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{8}{\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dit\tikzmorse@Dit}
\@morse@char{9}{\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dit}
\@morse@char{0}{\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah\tikzmorse@Dah}


\tikzset{morse/.style={thick,x=2pt}, morse draw/.style={}}
\newcommand\tikzmorse[2][]{\tikz[morse,#1] \@morse@draw{#2};}

\def\@morse@draw#1{%
    \edef\@morse@path{\@@morse@draw#1\@empty}
    \draw [morse draw] (0,0) \@morse@path;
}

\def\@@morse@draw#1{%
    \ifx\@empty#1\@empty
    \else
        \ifx,#1\@empty
            \tikzmorse@wordsep
        \else
            \csname @morse@char@#1\endcsname
            \tikzmorse@charsep
        \fi
        \expandafter\@@morse@draw
    \fi
}

\makeatother

\begin{document}

\tikzmorse{SOS}

\tikzmorse[ultra thick]{ABEEEEEEECD}

\tikzmorse{AB,CD}


\end{document}

enter image description here

0
0

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.

2
  • I'm not sure that I understand... Does this work? If so please provide a demonstration.
    – qubyte
    Feb 19, 2012 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. Feb 19, 2012 at 20:45
0
 \documentclass{article}
 \textwidth=15.5cm
 \oddsidemargin=-1.6cm
 \def\t#1{\textup{\bf{#1}}}
  \def\tb{\textbullet $\;$}
   \def\tn{\bf{-}$\;$}
 \begin{document}

 \huge
  Morse code for alphabets. Normally these contain 4 dots or dashes and their
  combinations.\\


  \vspace{1cm}

  \begin{tabular}{||c|c||c|c||c|c||c|c||}\hline
  $\t{a}$ & \tb  \tn & $\t{ b}$ & \tn  \tb \tb \tb & $\t{c}$& \tn \tb\tn \tb 
  &$\t{d}$&\tn\tb\tb \\\hline
  $\t{e}$&\tb&
   $\t{f}$&\tb\tb\tn\tb&$\t{g}$&\tn \tn\tb&$\t{h}$&\tb\tb\tb\tb\\\hline
   $\t{i}$&\tb\tb&$\t{j}$&\tb \tn\tn\tn&$\t{k}$&\tb\tn\tb&$\t{l}$&\tb\tn\tb\tb 
   \\\hline
    $\t{m}$ & \tn\tn & $\t{n}$ & \tb\tb\tn & $\t{o}$ & \tn\tn\tn & $\t{p}$ &  
    \tb\tn\tn\tb \\\hline
   $\t{q}$& \tn\tn\tb\tn & $\t{r}$ & \tb\tn\tb & $\t{s}$&\tb \tb\tb&$\t{t}$&  
   \tb\\\hline
   $\t{u}$&\tb \tb \tn& $\t{v}$ &\tb\tb\tb\tn&$\t{w}$&\tb \tn \tn & $\t{x}$&  
   \tn \tb\tb \tn\\\hline
   $\t{y}$ & \tn\tb \tn\tn & $\t{z}$ & \tn\tn\tb\tb & & & & \\\hline

   \end{tabular}\\
   \vspace{1cm}

        The numerals are generated by 5 \tb and \tn.

    \vspace{1cm}

    \begin{tabular}{||c|c||c|c||c|c||c|c||}\hline
     1 & \tb \tn\tn\tn\tn
     &2& \tb\tb\tn\tn\tn&3 & \tb\tb
    \tb\tn\tn & 4 & \tb \tb \tb \tb \tn\\\hline
    5 & \tb\tb\tb\tb\tb & 6 & \tn \tb \tb \tb \tb
      & 7& \tn\tn\tb\tb\tb & 8& \tn \tn \tn \tb \tb \\\hline
    9 & \tn \tn \tn \tn \tb & 0 & \tn\tn\tn\tn\tn && & & \\\hline

    \end{tabular}
    \newpage

     Other useful Morse codes.
    \vspace{1cm}

     \begin{tabular}{||p{1.7cm}|c||p{2.5cm}|c||p{1.8cm}|c||}\hline
     full stop .& \tb \tn \tb \tn \tb& comma , & \tn \tn \tb \tb \tn \tn& quest   
   mark ? & \tb \tb \tn \tn\tb \tn \\\hline  
   apos trophe ' & \tb \tn \tn \tn \tb& excl !& \tn \tb \tb \tb\tn & slash    
   /&\tn\tb\tb\tn\tb\\\hline
   open par " &\tn \tb \tn \tn \tb & close par & \tn\tb \tn \tn \tn \tn& \& &   
   \tb \tn \tb \tb \tb \\\hline
   semi colon ;&\tn\tb\tn\tb\tn\tb& equal sign =&\tn\tb\tb\tb\tn& plus sign +&   
   \tb\tn\tb\tn\tb \\\hline
    hyph- en -& \tn\tb\tb\tb\tn & under score&\tb\tb\tn\tn\tb\tn& ques- tion    
    mark ?&\tb\tn\tb\t\tn\tb\\\hline
    dolar sign \$ & \tb \tb \tb \tn \tb \tb \tn& amp @ & \tb\tn\tn\tb\tn\tb\tb&  
    &\\\hline


   \end{tabular}

    \vspace{1cm}

   prosings\\

    \begin{tabular}{||p{1.7cm}|c||p{2.0cm}|c||p{1.8cm}|c||}\hline
    end of work&\tb\tb\tb\tn\tb\tn& error & \tb\tb\tb\tb  \tb\tb\tb\tb& invi
     te to start&\tn \tb \tn\\\hline
   starting signal & \tn \tb \tn \tb \tb& new page & \tb \tn \tb \tn \tb& under   
    stood&\tb\tb\tb\tn\tb\\\hline
    wait & \tb \tb \tb \tb \tb&&&&\\\hline

    \end{tabular}

    \end{document}

    This is what I compiled for the Morse code after seeing a manual. It will    
    be useful.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .