3

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}
  • What encoding are you using for the TeX file? If it's UTF-8, then you can't use ¬ as a command char, because it's two-byte. – egreg May 2 '14 at 14:16
  • In any case, a verbatim command such as \inCodeStub can't go in the argument to another command. – egreg May 2 '14 at 14:36
  • Hi egreg. Thank you for your feedback. In fact, in my main document, I use \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 in fancyvrb.pdf hoping I could hack it, but I'm not a \TeXpert... yet. – Ricardo May 2 '14 at 16:02
  • If I add \usepackage[utf8]{inputenc} and save the example as UTF-8, I get errors at \CustomVerbatimCommand (which I expected). – egreg May 2 '14 at 16:13
3

You can't use ¬ and ¦ as “command chars” if you have \usepackage[utf8]{inputenc}. Only one byte characters can be used for the purpose (and not all of them, actually).

If you are willing to switch to XeLaTeX or LuaLaTeX, then it's possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.