我不知道是否verb
允许verbatim
进入\ifbool
检查。这使得检查非常有限。
我需要根据标志生成代码。要生成的代码恰好有一小部分使用了\verb
。因此我收到错误。
我在看文档但etoolbox
目前还没有看到任何可以帮助我解决这个问题的东西。
有没有办法使用\verb
如下
\documentclass[11pt]{scrbook}%
\IfFileExists{luatex85.sty}{\usepackage{luatex85}}{}
\usepackage{etoolbox}
\newbool{private}
\setbool{private}{true}
\begin{document}
\ifbool{private}
{
This is \verb|test| %how to make this work?
}%
{
This is the other case
}
\end{document}
编译
lualatex db.tex
This is LuaTeX, Version 0.95.0 (TeX Live 2016)
restricted system commands enabled.
(./db.tex
LaTeX2e <2016/03/31> patch level 2
Babel <3.9r> and hyphenation patterns for 1 language(s) loaded.
(/usr/local/texlive/2016/texmf-dist/tex/latex/koma-script/scrbook.cls
Document Class: scrbook 2016/06/14 v3.21 KOMA-Script document class (book)
(/usr/local/texlive/2016/texmf-dist/tex/latex/koma-script/scrkbase.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/koma-script/scrbase.sty
(/usr/local/texlive/2016/texmf-dist/tex/latex/graphics/keyval.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/koma-script/scrlfile.sty
Package scrlfile, 2016/06/14 v3.21 KOMA-Script package (loading files)
Copyright (C) Markus Kohm
))) (/usr/local/texlive/2016/texmf-dist/tex/latex/koma-script/tocbasic.sty)
(/usr/local/texlive/2016/texmf-dist/tex/latex/koma-script/scrsize11pt.clo)
(/usr/local/texlive/2016/texmf-dist/tex/latex/koma-script/typearea.sty
Package typearea, 2016/06/14 v3.21 KOMA-Script package (type area)
Copyright (C) Frank Neukam, 1992-1994
Copyright (C) Markus Kohm, 1994-
)) (/usr/local/texlive/2016/texmf-dist/tex/latex/etoolbox/etoolbox.sty)
(./db.aux)
! LaTeX Error: \verb ended by end of line.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.14 }
?
2016 年
更新
感谢以下答案。我必须使用,\newbool{private}\setbool{private}{true}
因为我已经使用复杂的预处理设置了这些(我在构建期间从文件中读取此标志等...并且我不想更改所有这些代码,因为它是自动化的),但在答案的帮助下,我找到了一种解决方法,它允许我使用当前的布尔值并用于\newif
绕过这个限制。这是最终的代码
\documentclass[11pt]{scrbook}%
\usepackage{etoolbox}
\newbool{private}
\setbool{private}{true}%in actual application, this only set at compile time
%from reading a file based on makefile command line
%setting. So it is not hardcoded in the Latex file as shown
%in this MWE. This is just an example.
\newif\ifprivate
\ifbool{private}{\privatetrue}{\privatefalse}
\begin{document}
\ifprivate
This is \verb|test|
\else
This is the other case
\fi
\end{document}
答案1
\ifbool
是宏,因此\verb
在其参数中是不可能的。
我认为标准\newif
方法要简单得多(并且有效),因为不涉及任何争论:
\documentclass[11pt]{scrbook}%
\IfFileExists{luatex85.sty}{\usepackage{luatex85}}{}
%\usepackage{etoolbox}
%\newbool{private}
%\setbool{private}{true}
\newif\ifprivate
\privatetrue
\begin{document}
\ifprivate
This is \verb|test| %how to make this work?
\else
This is the other case
\fi
\privatefalse% Now the other way round.
\ifprivate
This is \verb|test| %how to make this work?
\else
This is the other case
\fi
\end{document}
编辑:是的,我知道,这个问题是关于的etoolbox
,但我认为这更容易(或者根本不可能)并且它不需要其他包。
答案2
如果您事先知道要逐字排版的内容,verbbox
环境可以提供帮助。
\documentclass[11pt]{scrbook}%
\IfFileExists{luatex85.sty}{\usepackage{luatex85}}{}
\usepackage{etoolbox}
\newbool{private}
\setbool{private}{true}
\usepackage{verbatimbox}
\begin{document}
\begin{myverbbox}{\caseA}t&st\end{myverbbox}
\begin{myverbbox}{\caseB}Mi#tak^e\end{myverbbox}
\def\mytest{\ifbool{private}
{
This is \caseA %how to make this work?
}%
{
This is \caseB
}}
\mytest\par
\privatefalse
\mytest
\end{document}
注意,myverbbox
可以采用可选参数来调节逐字框。因此,如果将其定义为
\begin{myverbbox}[\scshape]{\caseA}T&st\end{myverbbox}
\begin{myverbbox}[\itshape]{\caseB}Mi#tak^e\end{myverbbox}
结果是