4

I would like to create a style for a command that takes multiple arguments:

\classify{A}{B}{C}

I started creating a style

Style Classify
    Category   FrontMatter

    LatexType  Command
    LatexName  classify
End

but this kind of style works only for commands that take a single argument. How can I modify the style definition so that it accepts multiple arguments and optional arguments?

2 Answers 2

4

This is not possible in LyX 2.0.x. LyX 2.1 provides a very flexible argument framework. LyX 2.1 will be released in a couple of months. You can use the development version (pretty stable) now alongside your current version on Ubuntu very easily with the LyX PPA.

To accomplish what you want to do in LyX 2.1, use the following:

Style Classify
    Category   FrontMatter
    LatexType  Command
    LatexName  refine
 Argument 1
    Mandatory 1
    LabelString "FirstMandatoryArg"
  EndArgument
 Argument 2
    Mandatory 1
    LabelString "SecondMandatoryArg"
  EndArgument
End

Attached is a screenshot that shows you how to use it. Note that the items are greyed out in the Insert menu because I've already inserted them. Note also that the order doesn't matter (thus A C B will still be {A}{B}{C}). You can see this in the LaTeX preview at the bottom of the screenshot.

enter image description here

5
  • What are the suggested workarounds for 2.0. I think that many other classes suffer the same problem, don't they?
    – gioele
    Commented May 16, 2013 at 20:43
  • 1
    @gioele As far as I know, ERT (that is, not using a layout) is the only workaround.
    – scottkosty
    Commented May 16, 2013 at 20:45
  • It is possible (see my answer), but this is the canonical way.
    – user24582
    Commented Sep 13, 2015 at 11:57
  • @scottkosty: Is there an option in LyX to have a macro that doesn't take an argument? Reference: How to do \hrulefill in LyX without a TeX inset
    – Werner
    Commented Jul 25, 2016 at 15:46
  • @Werner unfortunately I think not (but I'm not completely sure).
    – scottkosty
    Commented Jul 25, 2016 at 16:06
3

It is possible in LyX 2.0 using insets as named parameters inside of a frame inset. The downside is that there is less checking, so you might get side effects if you put other stuff like spaces or parskips in there.

My layout defines a Synopsis inset for 1 to 4 columns using tabulary (and ugly colors). Table heads and caption are optional. The Head and Text insets are supposed to be used multiple times.

Here are two examples, one with table heads and caption, and one without:

Synopsis inset in Lyx

The result:

Result in PDF viewer

The key is to first collect all necessary parameters and store them locally. Here's the layout code:

#\DeclareLyXModule{Synopsis} 
#DescriptionBegin 
#     Synopsis Inset Example for LyX 2.0
#     A frame inset with multiple, mostly optional named parameter insets.
#
#     Usage:
#         Synopsis[ SynopsisHead... SynopsisText... SynopsisCaption ]
#         Synopsis[ SynopsisHead+SynopsisText... SynopsisCaption ]
#
#     There must be 1..4 Texts, everything else is optional.
#     If you use Heads you must define one Head for each Text.
#     The 1st Head must be before the 1st Text.
#DescriptionEnd 

Format 21

Preamble

\usepackage{etoolbox}
\usepackage{tabulary}

\def\setSysnopsisColumnType#1{\newcolumntype{Q}{#1}}%

\long\def\synopsisFrame#1{%
    \begin{table}[h]%
        \small%
        %
        %%% execute parameter insets
        %%% (there should be nothing else, not even parskips!)
        #1%
        %
        %%% set table format
        \expandafter\setSysnopsisColumnType\expandafter{\theSynopsisFormat}%
        \begin{tabulary}{\textwidth}{Q}%
            %
            %%% table head (depends on body)
            \ifdef{\theSynopsisHeadi}{%
                \theSynopsisHeadi%
                \ifdef{\theSynopsisTextii}{ & \theSynopsisHeadii}{}%
                \ifdef{\theSynopsisTextiii}{ & \theSynopsisHeadiii}{}%
                \ifdef{\theSynopsisTextiv}{ & \theSynopsisHeadiv}{}%
                \\[1em]
            }{}%
            %
            %%% table body
            \theSynopsisTexti
            \ifdef{\theSynopsisTextii}{ & \theSynopsisTextii}{}%
            \ifdef{\theSynopsisTextiii}{ & \theSynopsisTextiii}{}%
            \ifdef{\theSynopsisTextiv}{ & \theSynopsisTextiv}{}%
            %
        \end{tabulary}%
        \ifdef{\theSynopsisCaption}{\caption{\theSynopsisCaption}}{}%
    \end{table}%
}

\long\def\synopsisHead#1{%
    %%% set next Head
    \ifdef{\theSynopsisHeadiv}{%
        \PackageError{SynopsisInset}{Too many Heads.}%
            {Only 4 Head insets are supported.}%
    }{%
        \ifdef{\theSynopsisHeadiii}{%
            \def\theSynopsisHeadiv{#1}%
        }{%
            \ifdef{\theSynopsisHeadii}{%
                \def\theSynopsisHeadiii{#1}%
            }{%
                \ifdef{\theSynopsisHeadi}{%
                    \def\theSynopsisHeadii{#1}%
                }{%
                    \def\theSynopsisHeadi{#1}%
                }%
            }%
        }%
    }%
}

\long\def\synopsisText#1{%
    %%% set next Text
    \ifdef{\theSynopsisTextiv}{%
        \PackageError{SynopsisInset}{Too many Texts.}%
            {Only 4 Text insets are supported.}%
    }{%
        \ifdef{\theSynopsisTextiii}{%
            \def\theSynopsisTextiv{#1}% set Text
            \def\theSynopsisFormat{|J|J|J|J|}% set format
        }{%
            \ifdef{\theSynopsisTextii}{%
                \def\theSynopsisTextiii{#1}%
                \def\theSynopsisFormat{|J|J|J|}%
            }{%
                \ifdef{\theSynopsisTexti}{%
                    \def\theSynopsisTextii{#1}%
                    \def\theSynopsisFormat{|J|J|}%
                }{%
                    \def\theSynopsisTexti{#1}%
                    \def\theSynopsisFormat{|J|}%
                }%
            }%
        }%
    }%
}

\long\def\synopsisCaption#1{%
    \def\theSynopsisCaption{#1}%
}

EndPreamble


InsetLayout Flex:Synopsis
    LyXType Custom
    LabelString Synopsis
    LatexType Command
    LatexName synopsisFrame
    BgColor blue
    ContentAsLabel 0
    CustomPars 1
    Decoration Classic
    LabelFont        
        Color        blue
        Size         Small
    EndFont
End

InsetLayout Flex:SynopsisHead
    LyXType Custom
    LabelString Head
    LatexType Command
    LatexName synopsisHead
    BgColor white
    ContentAsLabel 0
    CustomPars 1
    Decoration Classic
    LabelFont        
        Color        blue
        Size         Small
    EndFont
End

InsetLayout Flex:SynopsisText
    LyXType Custom
    LabelString Text
    LatexType Command
    LatexName synopsisText
    BgColor white
    ContentAsLabel 0
    CustomPars 1
    Decoration Classic
    LabelFont        
        Color        blue
        Size         Small
    EndFont
End

InsetLayout Flex:SynopsisCaption
    LyXType Custom
    LabelString Caption
    LatexType Command
    LatexName synopsisCaption
    BgColor white
    ContentAsLabel 0
    CustomPars 1
    Decoration Classic
    LabelFont        
        Color        blue
        Size         Small
    EndFont
End

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .