2

I am attempting to write a music related package where the user can enter chord names, that will be nicely printed:

% this prints out the C chord
\somecommand{C}

However, a chord names can have sharps and flats and I want the UI to be as simple as possible. Basically, avoid the need for the user to have to escape the # character, so he can write just this:

\somecommand{C#}

instead of this:

\somecommand{C\#}

In this answer, I read that this can be done with:

\catcode`#=12

So this, indeed, works:

\documentclass{article}
\newcommand{\mycommand}[1]
{
   chord=#1
}    
\catcode`#=12   
\begin{document}
\mycommand{G#}
\end{document}

And prints "chord=G#".

However, I am unable to make that trick work in the real situation, because all of this is actually embedded into a Tikz environment:

\documentclass{article}
\usepackage{tikz}

\newenvironment{myenv}
{
    \newcommand{\mycommand}[1]
    {
    \draw(0,0) node {chord=##1};
    }
    \begin{tikzpicture}
}
{
    \end{tikzpicture}
}  

\catcode`#=12

\begin{document}
\begin{myenv}
\mycommand{G#}
\end{myenv}
\end{document}

This MCVE produces lots of errors that I cannot understand:

ABD: EveryShipout initializing macros
(/usr/local/texlive/2017/texmf-dist/tex/context/base/mkii/supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
! Use of \@@mptopdf@@newabove doesn't match its definition.
l.136     \@@mptopdf@@newabove \csname n
                                        ewcount\endcsname \scratchcounter
If you say, e.g., `\def\a1{...}', then you must always
put `1' after `\a', since control sequence names are
made up of letters only. The macro here has not been
followed by the required stuff, so I'm ignoring it.

! Extra \endcsname.
l.136 ...opdf@@newabove \csname newcount\endcsname
...

Questions:

  • Can this be fixed in some way? How?
  • Or am I going the wrong way? Is there another path to achieve this goal?
  • 2
    off topic: you are defining \mycommand inside definition of myenv?! – Sigur Dec 27 '18 at 17:03
  • 2
    off topic 2: do you know \sharp? – Sigur Dec 27 '18 at 17:04
  • Q1: yes, because that command only makes sense inside the environment. Q2: no, thanks! – kebs Dec 27 '18 at 17:07
  • \# is U+0023 (#, number sign) which isn't really the same character as \sharp U+266F (♯, Sharp) are you sure that you just want to allow an unquoted # to typeset as itself? – David Carlisle Dec 27 '18 at 22:23
  • @DavidCarlisle The \sharp command (that I didn't know) looks indeed nicer but kinda too small, compared to the default rendering of \# (I mean in the readability sense, when seen from far away in a dark room). But I can consider these two options. – kebs Dec 28 '18 at 13:13
4

Delay the setting:

\documentclass{article}
\usepackage{tikz}

\newenvironment{myenv}
 {%
  \newcommand{\mycommand}[1]
    {%
    \draw(0,0) node {chord=##1};
    }%
    \begin{tikzpicture}
 }
 {%
    \end{tikzpicture}%
 }  

\AtBeginDocument{\catcode`#=12 }

\begin{document}
\begin{myenv}
\mycommand{G#}
\end{myenv}
\end{document}

It's a bad idea nonetheless. Use G\# and your life will be better.

  • Thanks for your answer! But about your last sentence, sure, I understand your point, the idea is just to avoid putting barriers in front of any potential new Latex users. No basic user should have to worry about those reserved characters (except maybe "%").The UI should be as simple as possible. – kebs Dec 27 '18 at 17:11
  • @kebs And potential errors as cryptical as possible… – TeXnician Dec 27 '18 at 17:17
  • @Texnician ;-) could be ! – kebs Dec 27 '18 at 17:18
  • Thinking again about this: could you give some hints on why exactly this is a bad idea ? Maybe some example of a situation where this could break something ? Because the package user isn't expected to define new commands, where this trick could be an issue. But maybe I miss something? – kebs Dec 27 '18 at 17:32
4
\documentclass{article}
\usepackage{tikz}

   \newcommand{\mycommand}[1]
    {
      \draw(0,0) node {chord=#1};
    }

\newenvironment{myenv}
{
 \catcode`\#=12
 \begin{tikzpicture}
}
{
 \end{tikzpicture}
}



\begin{document}
\begin{myenv}
\mycommand{G#}
\end{myenv}
\end{document}

enter image description here

  • Thanks for answer. Was pretty much simple, indeed... – kebs Dec 27 '18 at 17:22
  • @kebs try for example \fbox{\begin{myenv}...\end{myenv}} or any other command # won't work again, so you need (in principle) to document all the places where the syntax will or will not work instead of having a simple rule to say use \# (or better \sharp) everywhere. – David Carlisle Dec 27 '18 at 22:32
  • @DavidCarlisle Good point. But at present, the use case for the environment is pretty much straight forward, it just draws some lines, so there is no use case I see where a \fbox would be needed. But... who knows ? I will consider it anyway, thanks. – kebs Dec 27 '18 at 23:00

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.