Updated to work with strings instead of boxes
One idea is to create a "Duodecimal" style that automatically interprets strings as base 12 integers, and outputs the result in BaseForm
. This is an example stylesheet that does this:
SetOptions[
EvaluationNotebook[],
StyleDefinitions -> Notebook[{
Cell[StyleData[StyleDefinitions->"Default.nb"]],
Cell[StyleData["Duodecimal",StyleDefinitions->StyleData["Input"]],
CellEvaluationFunction->Function[
BaseForm[
With[
{
g = StringReplace[
#,
{
StartOfString~~i:DigitCharacter~~m:{DigitCharacter,"A","B"}..~~r___:>"12^^"<>i<>m<>" "<>r,
StartOfString~~m:{DigitCharacter,"A","B"}..~~EndOfString:>"12^^"<>m
}
]&
},
ToExpression @ StringReplace[
#,
{
f:(DigitCharacter..~~"."~~DigitCharacter...):>f,
l:WordCharacter.. :> g[l]
}
]
],
12
]
],
DefaultFormatType->"RawInputForm"
]
},
StyleDefinitions->"PrivateStylesheetFormatting.nb"]
]
Note that I use DefaultFormatType->"RawInputForm"
so that there is no space between the 1 and the A in an input like "1A". Both InputForm
and StandardForm
will display an input like 1A with a space.
Using "RawInputForm" means that the input is a simple string (as you can tell by using Show Expression
) from the Cell
menu. I originally tried to convert the string to boxes, and then processed the boxes so that the input was in base 12. However, this didn't quite work (there were issues with both multiplication and powers), so now I instead process the input string. I avoided processing real numbers (as indicated by a .
) and I converted words to a base 12 string if the word started with an integer or the whole word consisted strictly of duodecimal characters.
Finally, I included a BaseForm
wrapper in the output. You can always eliminate this and use your $Post method instead.
To use this style, enter Alt-0 and type in Duodecimal. If desired, one could also give it a MenuCommandKey
, or add a StyleKeyMapping
to the "Input" style.
Here is a screen shot of a couple examples:
