I have a command definition in my LaTeX preamble:
\newcommand*{\code}[1]{\texttt{#1}}
But the usage of this command appears as an ERT e.g. \code{select}
. I would like this to appear as select that is, italics. How to do this?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityYou need to tell Lyx how it is to mark up the command, by changing the layout you use, or, probably the right thing, defining a module.
Rob Oakes has written a weblog post, Customizing Lyx: An Introduction to Modules. His explanation make it look quite simple to do, although I've never tried.
I was able to extract my answer from Rob's another article here: Close your lyx file from within lyx editor. Put the following near the top of your lyx file after \end_modules
using a text editor. Reopen the lyx file using the lyx editor. Then you can customize a part of your text by: Select text > Right Click > Text Styles > Charstyle:code.
\begin_local_layout
Format 7
InsetLayout CharStyle:Code
LyxType charstyle
LabelString code
LatexType command
LatexName code
Font
Family Sans
Color Green
EndFont
Preamble
\newcommand*{\code}[1]{\texttt{#1}}
EndPreamble
End
\end_local_layout
To use the same layout for a different document, use File > New from Template.
Even though it's been some years, I think it makes sense to add here how to add custom citation commands to LyX. I did this in order to use the biblatex commands \parencite
and \textcite
after including biblatex support in LyX following this guide on LyX wiki
Because the changes to Options>Local Layout
which are mentioned in Section "Note on the natbib compatibility mode:" there didn't work for me, (LyX 2.2.1), I included support for the two commands mentioned above by myself.
The approach was inspired (in fact, much was copy'n'pasted) from /usr/share/lyx/layouts/natbib.module
The following listing describes to LyX how to display a citation type and which LaTeX command to use for it. Look into the LyX manual about customization (Help>Customization), section "5.3.12 Citation format description" to get a bit of detail about the used language. To summarize, expressions like %author%
print a bibliography field, !macroname DEF
define a macro which gets replaced by DEF
if writing %!macroname%
somewhere. %fieldname%[[SOMETHING]][[ELSE]]
are conditional expressions where SOMETHING
is only printed if %fieldname%
is not empty, else ELSE
is printed (or nothing if the second [[...]]
statement is omitted). %next%
is relevant if there are multiple citations selected in the LyX citation dialogue. It is not empty for as many calls as there are citations and each call to %next%
changes the other fields like %author%
to the next citation. Everything after #
is a comment.
I added the following to Options>Local Layout
:
CiteEngine default
# [][] indicate that this command accepts pre- and post-text if set
# in the LyX dialog ("see" and "p. 123" in
# "(see Miller, 1990, p. 123)")
# This also states which LaTeX command must be used.
# Makes an error in LyX cite dialog. Commands seemingly must
# start with 'cite...'
#parencite[][]
# Put \let\citep\parencite in document preamble
citep[][]
End
CiteFormat default
# Macros to print user text if specified in LyX dialog
!textbefore {%textbefore%[[%textbefore% ]]}
!textafter {%textafter%[[, %textafter%]]}
# Macros to print bibtex fields or '??' if bibtex field is empty
!abbrvauthor {%abbrvauthor%[[%abbrvauthor%]][[??]]}
!year {%year%[[%year%]][[??]]}{%modifier%[[%modifier%]]}
# Opening and closing characters. Useful to define as macros if
# multiple citation commands are defined
!open (
!sep ;
!close )
# These two call each other until there are no citations left
!citealp %!abbrvauthor%, %!year%%!nextcitealp%
!nextcitealp {%next%[[%!sep% %!citealp%]]}
# Main definition
citep %!open%%!textbefore%%!citealp%%!textafter%%!close%
# May include additional commands if added in `CiteEngine` section
# as well. See /usr/share/lyx/layouts/natbib.layout
# citet %!open%%!textbefore%%!citealt%%!textafter%%!close%
End
I added the biblatex import statement into my documents' preamble, as mentioned in the link above. I get an error if defining parencite
directly in the Local Format as stated in comment above, therefore I added an alias called \citep
.
% --- As advised in wiki article linked above -----------------------
\usepackage{polyglossia} % Load if using biblatex with {Xe,Lua}LaTeX
\usepackage[style=authoryear,backend=biber]{biblatex}
\addbibresource{my.bib} % Put your .bib file here
% -------------------------------------------------------------------
% Add the alias for \parencite like mentioned above
% \citep now means the same as \parencite
\let\citep\parencite
You can also copy the text which @phaedrus wrote in his answer to the Field in Document > Settings... > Local Layout. Use this portion of his code:
InsetLayout CharStyle:Code
LyxType charstyle
LabelString code
LatexType command
LatexName code
Font
Family Sans
Color Green
EndFont
Preamble
\newcommand*{\code}[1]{\texttt{#1}}
EndPreamble
End
search for "InsetLayout" in the Help > Customization manual to get explanation.
\texttt
with\textit
solve your problem?