I've just registered. This is my first question to this site.
I am trying to make a verbatim command for inline code. This has been asked and answered many times before, but I never found a direct answer that actually works for me. I am using MikTeX 2.9, completely updated.
In my text - a technical manual - I am using fancyvrb
. I have plain text and code snippets. These are handled by \DefineVerbatimEnvironment
. It is working fine.
Inline code, on the other hand, has been posing the usual problem of passing a parameter into the \Verb
. I mean to have an inline environment suitable for code, but allowing for text formatting AND immune to backslashes and so, so that I can format inline code or Windows file paths without the need to encode every single backslash as \textbackslash
.
I can use \CustomVerbatimCommand
, but this uses a single character for enclosing the target text, instead of curly brackets. I came up with this definition:
\CustomVerbatimCommand{\inCodeStub}{Verb}{commandchars=¬¦^} \newcommand{\inCode}[1]{\inCodeStub¡#1¡}
With this I can use the \inCodeStub¡text¡
- which I did not wish to - but the attempt at the bracketed version is not working. Could anyone tell me why/offer a solution?
Here is a MWE:
\documentclass{book}
\usepackage{fancyvrb}
\usepackage{inconsolata}
% New command/environment for code snippets
% Inline code fragments
\newcommand\InCode[1]{\texttt{\frenchspacing#1}}
% Code blocks
\newenvironment{Code}{
\begin{quote}\begin{tt}
}{\end{tt}\end{quote}}
% Code blocks, no left margin
\newenvironment{leftCode}{
\begin{tt}
}{\end{tt}}
% Complex code blocks, where too many encodings would be necessary
% Inline code fragments
% The inverted exclamation mark -- ¡ -- never occurs in code, so it is a good candidate to use as bracket
\CustomVerbatimCommand{\inCodeStub}{Verb}{commandchars=¬¦^}
\newcommand{\inCode}[1]{\inCodeStub¡#1¡}
% This is a stub for the fancyvrb starred environments below
\DefineVerbatimEnvironment%
{leftCodeStub}{Verbatim}{commandchars=¬¦^}
% NOTE: Inside this environment, intead of
% \command{object}, write
% ¬command¦object^
% as these characters -- ¬, ¦ and ^ -- are very rare in code
\newenvironment{Code*}{
\quote\leftCodeStub
}{\endleftCodeStub\endquote}
\begin{document}
Text outside. \inCode{Text inside. Works with plain text.} Text outside.
Now with backslashes it doesn't work: \inCode{\something and ¬textbf¦\something^}
But the same using the stub code works fine: \inCodeStub¡\something and ¬textbf¦\something^¡
\end{document}
¬
as a command char, because it's two-byte.\inCodeStub
can't go in the argument to another command.\usepackage[utf8]{inputenc}
and I still manage to have this working with\inCodeStub¡\something and ¬textbf¦\something^¡
. The only part not working is wrapping this in a functional command, with{}
s. I checked the\CustomVerbatimCommand
definition infancyvrb.pdf
hoping I could hack it, but I'm not a \TeXpert... yet.\usepackage[utf8]{inputenc}
and save the example as UTF-8, I get errors at\CustomVerbatimCommand
(which I expected).