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.

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}
  • What is the upside of writing this yourself vs the morse package below? – Canageek Sep 24 '11 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. – Yiannis Lazarides Sep 25 '11 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.

  • Letters seem to match with what Wikipedia says, punctuation doesn't. – Caramdir May 26 '11 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

  • 404 - Object does not exist – Henri Menke Apr 2 '18 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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.