| 1 |
% !TeX root = ../easyfloats.tex |
| 2 |
|
| 3 |
% Copyright © 2020 E. Zöllner |
| 4 |
% Alternatively to the terms of the LPPL, at your choice, |
| 5 |
% you can redistribute and/or modify this file under the |
| 6 |
% terms of the Do What The Fuck You Want To Public License, Version 2, |
| 7 |
% as published by Sam Hocevar. See http://www.wtfpl.net/about/. |
| 8 |
|
| 9 |
% Allows markdown like syntax: |
| 10 |
% *emph* |
| 11 |
% `code` |
| 12 |
% |
| 13 |
% If `<code>` is used in an argument <code> cannot contain: |
| 14 |
% - comment characters (percent sign) |
| 15 |
% - unbalanced curly braces |
| 16 |
% |
| 17 |
% If `<code>` is used in a context where fragile commands break |
| 18 |
% (e.g. an entry of the table of contents or list of figures): |
| 19 |
% - control sequences and active characters *after* the first token |
| 20 |
% must be preceded by \string or \protect |
| 21 |
% |
| 22 |
% If `<code>` is used in a pdf string (e.g. in \section): |
| 23 |
% - control sequences *after* the first token must be preceded by \cmd |
| 24 |
% - active characters *after* the first token must be preceded by \string |
| 25 |
% - curly braces *after* the first token must be escaped or preceded by \string |
| 26 |
% |
| 27 |
% If you add prefixes like `\string` or `\cmd` in a normal execution context |
| 28 |
% they will (of course) be treated as code to be displayed. |
| 29 |
% Therefore it may be necessary to pass different code for these three cases: |
| 30 |
% \section[\texorpdfstring |
| 31 |
% {`\emph{page\string~42}`} |
| 32 |
% {`\emph\{page\string~42\}`}] |
| 33 |
% {`\emph{page~42}`} |
| 34 |
% |
| 35 |
% Warning: \section uses \write without \immediate. |
| 36 |
% If the definition of ` is different where it is written |
| 37 |
% e.g. if the next page break is inside of a minted environment |
| 38 |
% you will get into trouble. |
| 39 |
% |
| 40 |
% Warning: With * being an active character you cannot just |
| 41 |
% use starred versions of environments. |
| 42 |
% \starred expands to a * with catcode other: |
| 43 |
% \begin{examplecode\starred} |
| 44 |
% <code> |
| 45 |
% \end{examplecode\starred} |
| 46 |
% I am redefining \@ifstar to recognize active * as well. |
| 47 |
% So starred commands can be used usual. |
| 48 |
% |
| 49 |
% Warning: This should be loaded last so that the |
| 50 |
% category changing code is appended to the hook last. |
| 51 |
% Otherwise there is trouble with minted. |
| 52 |
% |
| 53 |
% depends on |
| 54 |
% \LinkKeyword{<keywordtype>}{<keyword>}<formatmacro>{<text>} |
| 55 |
% from preamble/keydoc.tex |
| 56 |
|
| 57 |
\usepackage{etoolbox} |
| 58 |
|
| 59 |
\newcommand{\starred}{*} |
| 60 |
|
| 61 |
\providecommand{\mintinline}[1]{\verb}%} |
| 62 |
|
| 63 |
\makeatletter |
| 64 |
% An alternative version of \@ifstar which also recognizes an active star. |
| 65 |
% I am not changing \@ifstar here because the active * is defined in \AtBeginDocument. |
| 66 |
% So in the preamble \newcommand would check if the next token is an undefined control sequence token. |
| 67 |
% If it was it would be discarded and it would try to define the next token. |
| 68 |
\let\@markdown@original@ifstar=\@ifstar |
| 69 |
\catcode`*=\active |
| 70 |
\def\@markdown@ifstar#1#2{% |
| 71 |
\@markdown@original@ifstar |
| 72 |
{#1}% |
| 73 |
{% |
| 74 |
\@ifnextchar*% |
| 75 |
{\@firstoftwo{#1}}% |
| 76 |
{#2}% |
| 77 |
}% |
| 78 |
} |
| 79 |
\@makeother* |
| 80 |
|
| 81 |
\protected\def\IfProtectedContextGobble#1{\ifx\@thirdofthree#1\relax\else#1\fi} |
| 82 |
\newcommand\IfUnprotectedContext{% |
| 83 |
\IfProtectedContextGobble\@thirdofthree |
| 84 |
\@firstoftwo |
| 85 |
} |
| 86 |
|
| 87 |
\catcode``=\active |
| 88 |
\newcommand\MarkdownVerb{% |
| 89 |
\begingroup |
| 90 |
\@examplecode@setcatcodes |
| 91 |
\catcode\string``=\active |
| 92 |
\futurelet \@MarkdownVerb@nexttoken \@MarkdownVerb@do |
| 93 |
} |
| 94 |
\long\def\@MarkdownVerb@do#1`{% |
| 95 |
\IfKeywordUndefined{}{\detokenize{#1}}{% |
| 96 |
\ifcat \noexpand\@MarkdownVerb@nexttoken a% |
| 97 |
\xckey`#1`% |
| 98 |
\else |
| 99 |
\xc`#1`% |
| 100 |
\fi |
| 101 |
}{% |
| 102 |
\catcode\string` =10 |
| 103 |
\catcode\string`\^^M=9 |
| 104 |
\def\do{\LinkKeyword{}{\detokenize{#1}}\@firstofone\@firstofone}% |
| 105 |
\scantokens{\appto\do{{#1}}}% |
| 106 |
\do |
| 107 |
}% |
| 108 |
\endgroup |
| 109 |
} |
| 110 |
\def\StringifyMarkdownVerb#1`{\string#1\noexpand`} |
| 111 |
\def\PdfStringifyMarkdownVerb#1#2`{% |
| 112 |
\ifcat\relax\noexpand#1\relax |
| 113 |
\cmd#1% |
| 114 |
\else |
| 115 |
\string#1% |
| 116 |
\fi |
| 117 |
\detokenize{#2}% |
| 118 |
} |
| 119 |
\@makeother` |
| 120 |
|
| 121 |
\write\@auxout{\string\catcode\string\string``=\active} |
| 122 |
|
| 123 |
% \AfterEndPreamble is important when used with minted |
| 124 |
\AfterEndPreamble{% |
| 125 |
\catcode`*=\active |
| 126 |
\appto\dospecials{\do\*}% |
| 127 |
\scantokens{\def\ActiveAsterisc{*}}% |
| 128 |
\edef\do{\def\expandonce\ActiveAsterisc##1\expandonce\ActiveAsterisc{\noexpand\emph{##1}}}% |
| 129 |
\do |
| 130 |
\let\@ifstar=\@markdown@ifstar |
| 131 |
% |
| 132 |
\catcode``=\active |
| 133 |
%% If I add ` to \dospecials \mintinline will change it to catcode other |
| 134 |
%% therefore not finding the active ` which marks the end resulting in |
| 135 |
%% ! Paragraph ended before \minted@inline@ii was complete. |
| 136 |
%% I could change the definition of \mintinline to expect an other ` |
| 137 |
%% but that would fail when using `...` inside of an argument. |
| 138 |
%% Whether ` is contained in \dospecials or not does not seem to bother \verb. |
| 139 |
%\appto\dospecials{\do\`}% |
| 140 |
\scantokens{% |
| 141 |
\def`{% |
| 142 |
\texorpdfstring{% |
| 143 |
\IfUnprotectedContext% |
| 144 |
{\MarkdownVerb} |
| 145 |
{\noexpand`\StringifyMarkdownVerb}% |
| 146 |
}{% |
| 147 |
\PdfStringifyMarkdownVerb |
| 148 |
}% |
| 149 |
}% |
| 150 |
}% |
| 151 |
} |
| 152 |
|
| 153 |
\AtEndDocument{% |
| 154 |
\@makeother* |
| 155 |
} |
| 156 |
|
| 157 |
\makeatother |