From b74b2374e23c9c46a2a65c2f441afca22b15cf25 Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Mon, 7 May 2018 20:44:40 -0400 Subject: [PATCH] \copyright, \textregistered, \textcircled support (#1073) * \copyright support * Implement \textcircled, \copyright, \textregistered --- src/functions/accent.js | 31 ++++++++++++++++++++++++------- src/katex.less | 7 ++++++- src/macros.js | 10 ++++++++++ src/symbols.js | 1 + 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/functions/accent.js b/src/functions/accent.js index 0388dea6..f5b4ca30 100644 --- a/src/functions/accent.js +++ b/src/functions/accent.js @@ -63,7 +63,7 @@ const htmlBuilder = (group, options) => { } // calculate the amount of space between the body and the accent - const clearance = Math.min( + let clearance = Math.min( body.height, options.fontMetrics().xHeight); @@ -91,17 +91,34 @@ const htmlBuilder = (group, options) => { accentBody = buildCommon.makeSpan(["accent-body"], [accent]); - // CSS defines `.katex .accent .accent-body { width: 0 }` + // "Full" accents expand the width of the resulting symbol to be + // at least the width of the accent, and overlap directly onto the + // character without any vertical offset. + const accentFull = (group.value.label === "\\textcircled"); + if (accentFull) { + accentBody.classes.push('accent-full'); + clearance = body.height; + } + + // Shift the accent over by the skew. + let left = skew; + + // CSS defines `.katex .accent .accent-body:not(.accent-full) { width: 0 }` // so that the accent doesn't contribute to the bounding box. // We need to shift the character by its width (effectively half // its width) to compensate. - let left = -width / 2; - - // Shift the accent over by the skew. - left += skew; + if (!accentFull) { + left -= width / 2; + } accentBody.style.left = left + "em"; + // \textcircled uses the \bigcirc glyph, so it needs some + // vertical adjustment to match LaTeX. + if (group.value.label === "\\textcircled") { + accentBody.style.top = ".2em"; + } + accentBody = buildCommon.makeVList({ positionType: "firstBaseline", children: [ @@ -215,7 +232,7 @@ defineFunction({ type: "accent", names: [ "\\'", "\\`", "\\^", "\\~", "\\=", "\\u", "\\.", '\\"', - "\\r", "\\H", "\\v", + "\\r", "\\H", "\\v", "\\textcircled", ], props: { numArgs: 1, diff --git a/src/katex.less b/src/katex.less index 07a066a9..6f55a213 100644 --- a/src/katex.less +++ b/src/katex.less @@ -401,8 +401,13 @@ text-align: center; } - .accent-body { + // Accents that are not of the accent-full class have zero width + // (do not contribute to the width of the final symbol). + .accent-body:not(.accent-full) { width: 0; + } + + .accent-body { position: relative; // so that 'left' can shift the accent } } diff --git a/src/macros.js b/src/macros.js index 85f53284..8dbf8f62 100644 --- a/src/macros.js +++ b/src/macros.js @@ -117,6 +117,16 @@ defineMacro("\\rbrack", "]"); defineMacro("\\aa", "\\r a"); defineMacro("\\AA", "\\r A"); +// \DeclareTextCommandDefault{\textcopyright}{\textcircled{c}} +// \DeclareTextCommandDefault{\textregistered}{\textcircled{% +// \check@mathfonts\fontsize\sf@size\z@\math@fontsfalse\selectfont R}} +// \DeclareRobustCommand{\copyright}{% +// \ifmmode{\nfss@text{\textcopyright}}\else\textcopyright\fi} +defineMacro("\\textcopyright", "\\textcircled{c}"); +defineMacro("\\copyright", + "\\TextOrMath{\\textcopyright}{\\text{\\textcopyright}}"); +defineMacro("\\textregistered", "\\textcircled{\\scriptsize R}"); + // Unicode double-struck letters defineMacro("\u2102", "\\mathbb{C}"); defineMacro("\u210D", "\\mathbb{H}"); diff --git a/src/symbols.js b/src/symbols.js index 58160414..87fc91e2 100644 --- a/src/symbols.js +++ b/src/symbols.js @@ -697,6 +697,7 @@ defineSymbol(text, main, accent, "\u02da", "\\r"); // ring above defineSymbol(text, main, accent, "\u02c7", "\\v"); // caron defineSymbol(text, main, accent, "\u00a8", '\\"'); // diaresis defineSymbol(text, main, accent, "\u02dd", "\\H"); // double acute +defineSymbol(text, main, accent, "\u25ef", "\\textcircled"); // \bigcirc glyph defineSymbol(text, main, textord, "\u2013", "--"); defineSymbol(text, main, textord, "\u2013", "\\textendash");