| 1 |
%%! TEX program = lualatex |
| 2 |
\ProvidesFile{cprotectinside.tex} [2022/06/25 v0.0.0 Use cprotect arbitrarily deeply nested] |
| 3 |
\documentclass{l3doc} |
| 4 |
\EnableCrossrefs |
| 5 |
\CodelineIndex |
| 6 |
\RecordChanges |
| 7 |
\usepackage{hyperref} |
| 8 |
\usepackage{csquotes} |
| 9 |
%\MakeOuterQuote{"} |
| 10 |
\begin{document} |
| 11 |
\GetFileInfo{\jobname.tex} |
| 12 |
|
| 13 |
\title{\textsf{cprotectinside} --- Use cprotect arbitrarily deeply nested |
| 14 |
\thanks{This file describes version \fileversion, last revised \filedate.} |
| 15 |
} |
| 16 |
\author{user202729% |
| 17 |
%\thanks{E-mail: (not set)} |
| 18 |
} |
| 19 |
\date{Released \filedate} |
| 20 |
|
| 21 |
\maketitle |
| 22 |
|
| 23 |
\changes{v0.0.0}{2022/05/18}{First public release} |
| 24 |
|
| 25 |
\let\backtick=` |
| 26 |
\MakeShortVerb{`} |
| 27 |
|
| 28 |
\begin{abstract} |
| 29 |
Extends on `cprotect` package to allow users to use verbatim-like commands inside arbitrary |
| 30 |
parameters. |
| 31 |
|
| 32 |
\end{abstract} |
| 33 |
|
| 34 |
\section{Usage} |
| 35 |
|
| 36 |
|
| 37 |
\subsection{Main function} |
| 38 |
\begin{function}{\cprotectinside} |
| 39 |
\begin{syntax} |
| 40 |
\cs{cprotectinside} \meta{delimiter} \meta{content} \\ |
| 41 |
\end{syntax} |
| 42 |
Execute \meta{content} that possibly contains verbatim content. |
| 43 |
|
| 44 |
This is a bit hard to explain. To give an example: |
| 45 |
|
| 46 |
Take an example from \url{https://tex.stackexchange.com/q/24574}, |
| 47 |
you want to write \verb+\textbf{using \verb|-i| as}+. Unfortunately, `\textbf` does not accept |
| 48 |
`\verb` %` %workaround for syntax highlighter |
| 49 |
in input. |
| 50 |
|
| 51 |
Then you need to |
| 52 |
|
| 53 |
\begin{itemize} |
| 54 |
\item Wrap the whole code inside `\cprotectinside{!}{ ... }` |
| 55 |
\item Wrap each `\verb` %` |
| 56 |
command (and its arguments) inside `!...!`. |
| 57 |
\end{itemize} |
| 58 |
|
| 59 |
The resulting code would be: |
| 60 |
|
| 61 |
\begin{verbatim} |
| 62 |
\cprotectinside{!}{\textbf{\using !\verb|-i|! as}}} |
| 63 |
\end{verbatim} |
| 64 |
|
| 65 |
The first argument `!` is the delimiter, it can be changed as long as it's changed correspondingly in the \meta{content} part. |
| 66 |
|
| 67 |
In this simple case, it's possible to simply use `cprotect`. Nevertheless this package is useful in some cases, for example... |
| 68 |
|
| 69 |
\begin{itemize} |
| 70 |
\item Value of keyval argument: |
| 71 |
|
| 72 |
\begin{verbatim} |
| 73 |
\cprotectinside{*}{ |
| 74 |
\begin{lstlisting}[language=Perl, |
| 75 |
caption={text *\verb+myverb+* some more text}] |
| 76 |
code code |
| 77 |
code code |
| 78 |
\end{lstlisting} |
| 79 |
} |
| 80 |
\end{verbatim} |
| 81 |
|
| 82 |
\item Pseudo-environments (contrived example): |
| 83 |
|
| 84 |
\begin{verbatim} |
| 85 |
\cprotectinside{*}{ |
| 86 |
\begin{align} |
| 87 |
1 &= 2+\text{text *\verb+text+* text} |
| 88 |
\end{align} |
| 89 |
} |
| 90 |
\end{verbatim} |
| 91 |
\end{itemize} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
\end{function} |
| 96 |
|
| 97 |
|
| 98 |
\subsection{Common issues} |
| 99 |
|
| 100 |
\begin{itemize} |
| 101 |
|
| 102 |
\item If you get the error message |
| 103 |
|
| 104 |
\begin{quote} |
| 105 |
LaTeX cmd Error: Verbatim-like command \backtick `\cprotectinside`' illegal in argument. |
| 106 |
\end{quote} |
| 107 |
|
| 108 |
You can't use a literal TAB character inside \meta{content} |
| 109 |
because of a known bug: \url{https://tex.stackexchange.com/q/508001} |
| 110 |
|
| 111 |
(when the tab character is at the begin of a line -- which is the most common case, it will give no error message but they will be removed from the code. Which might matter for `verbatim` environments, for example.) |
| 112 |
|
| 113 |
\item Note that the following code will not work as desired |
| 114 |
\begin{verbatim} |
| 115 |
\cprotectinside{!}{ |
| 116 |
some content |
| 117 |
%} |
| 118 |
some other content |
| 119 |
} |
| 120 |
\end{verbatim} |
| 121 |
because of the unbalanced brace in the comment. It's possible to use characters different from braces to delimit the second argument |
| 122 |
\begin{verbatim} |
| 123 |
\cprotectinside{!}| |
| 124 |
some content |
| 125 |
%} |
| 126 |
some other content |
| 127 |
| |
| 128 |
\end{verbatim} |
| 129 |
or use the workarounds described in the documentation of \texttt{cprotect} package. |
| 130 |
|
| 131 |
\item Contents that is intended to be passed as |
| 132 |
\enquote{text} |
| 133 |
to the outer command must not be `cprotect`ed. |
| 134 |
|
| 135 |
As a general rule of thumb, if the content inside can be replaced with a `\includegraphics` with no compilation error, |
| 136 |
it can be `cprotect`ed. |
| 137 |
|
| 138 |
|
| 139 |
\end{itemize} |
| 140 |
|
| 141 |
\subsection{Implementation note} |
| 142 |
|
| 143 |
The working of the code is similar to how \texttt{cprotect} package works. Described in more details |
| 144 |
in \url{https://tex.stackexchange.com/q/622512} (post by the package author). |
| 145 |
|
| 146 |
In particular, given the code |
| 147 |
\verb+\textbf{using \verb|-i| as}+ |
| 148 |
the command might transform it to become |
| 149 |
\verb+\textbf{using \cpiContentAi/ as}+ |
| 150 |
then execute the resulting code. |
| 151 |
Where `\cpiContentAi/` is defined to be something similar to `\input{inner.tex}`, with the (imaginary) file |
| 152 |
`inner.tex` has the content `\verb|-i|\empty`. |
| 153 |
|
| 154 |
The string to be appended inside each inner chunk defaults to `\empty`, used to remove the space generated at the |
| 155 |
end of each file (similar in purpose to `^^E^^L` used by `cprotect` package), but it can be configured: |
| 156 |
|
| 157 |
\begin{variable}{\cprotectinsideAppend} |
| 158 |
The content to be appended to every inner macro. Should already be detokenized. Defaults to the detokenized string \texttt{\textbackslash empty}. |
| 159 |
\end{variable} |
| 160 |
|
| 161 |
|
| 162 |
\StopEventually{% |
| 163 |
\PrintChanges |
| 164 |
\PrintIndex |
| 165 |
} |
| 166 |
\Finale |
| 167 |
\end{document} |