How do I make a transformer in LTspice?
Although it is very possible in LTspice to make to make dedicated subcircuits for specific transformers, the preferred method of making a generic transformer when drafting a simulation schematic is to simply place a separate inductor for each separate transformer winding and then couple them all together magnetically via a single mutual inductance (K) statement placed as a SPICE directive on the schematic. See the Help file Circuit Element section on Mutual Inductance for detailed syntax. Note that inductors called out in a mutual inductance statement will be automatically given a phasing dot if one does not already exist.
When creating a new transformer this way, especially for use in a switched mode power circuit, it is generally best to first specify the mutual coupling coefficient to be exactly 1. By starting with 100 percent coupling there will be no leakage inductance in any winding and this will minimize the likelihood of the windings ringing at extremely high frequencies (which can slow the simulation to a crawl at each switching edge). However, be aware that a mutual inductance value of plus (or minus) unity may lead to simulation difficulties if Skip the initial operating point solution (UIC) is specified for the .tran command (specifying a realistic resistance for each inductor "winding" - ctrl-right-mouse-click - will minimize this tendency).
Note that when coupled inductors are used as transformer windings, individual winding inductances rather than turns ratios must be specified (inductance ratios should be proportional to the square of the turns ratios).
If I want to model leakage inductance, how should I do that?
The most flexible method is to keep the windings' mutual inductance statement at unity and add a small discrete leakage inductance in series with each winding. This is the most straightforward way to model transformers with asymmetrical leakage inductances, however, if your transformer is electrically symmetrical, it may be more convenient to simply set the mutual inductance to a value less than one. For each winding the resulting leakage inductance will be (1-K) times that winding's inductance.