<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <!-- header_tag --> <html lang="en"> <head> <title>Saving typing with variables and functions - GNU LilyPond Learning Manual</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="description" content="GNU LilyPond Learning Manual"> <meta name="generator" content="makeinfo 4.11"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Suggestions-for-writing-LilyPond-input-files.html#Suggestions-for-writing-LilyPond-input-files" title="Suggestions for writing LilyPond input files"> <link rel="prev" href="Large-projects.html#Large-projects" title="Large projects"> <link rel="next" href="Style-sheets.html#Style-sheets" title="Style sheets"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <!-- Copyright (C) 1999--2007 by the authors Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections. A copy of the license is included in the section entitled ``GNU Free Documentation License''. --> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } hr { border:0; height:1; color: #000000; background-color: #000000; } /* hr { border: none; height: 1px; color: #666666; background-color: #666666; } body { border-left: 1px solid #666666; border-right: 1px solid #666666; color: #332d28; margin-right: auto; margin-left: auto; width: 60em; list-style-type: square; font-family: Arial,Helvetica,sans-serif; padding-right: 1em; padding-left: 1em; } a { border-bottom: 1px dashed #344242; text-decoration: none; color: #344242; } a:link { text-decoration: none; } a:visited { border-bottom: 1px dashed #666666; color: #666666; } a:active { border-bottom: 1px solid #00cccc; color: #00cccc; } a:hover { border-bottom: 1px solid #1d7b85; color: #1d7b85; } blockquote { border: 1px solid #cccccc; padding: 3px; width: 40em; } .node { border-left: 1px solid #666666; margin: -0.5em 0px 1em; padding: 2px 1px 0px; font-style: italic; } .node a { border: none; text-decoration: underline; font-style: normal; font-weight: bold; } .verbatim { font-family: "Courier New",Courier,monospace; } .unnumberedsubsubsec { font-size: large; color: #1d7b85; } .subsubheading { font-size: large; color: #3b220d; } .contents { border: 1px dashed #339999; margin: 3px 2em; list-style-type: square; padding-right: 1em; width: 40em; background-color: #fcfff9; } .contents a { border-bottom: 1px dashed #423d34; text-decoration: none; color: #423d34; } .contents a:visited { border-bottom: 1px dashed #666666; color: #666666; } .contents a:active { border-bottom: 1px solid #f0d86d; color: #f0d86d; } .contents a:hover { border-bottom: 1px solid #3b220d; color: #3b220d; } .menu { border-left: 1px dashed #339999; margin: 3px 2em 1em; list-style-type: square; padding-left: 1.4em; width: 40em; } .unnumbered { } h2 { font-size: x-large; color: #1d7b85; } */ --></style> </head> <BODY BGCOLOR=WHITE TEXT=BLACK> <div class="node"> <p> <a name="Saving-typing-with-variables-and-functions"></a> Next: <a rel="next" accesskey="n" href="Style-sheets.html#Style-sheets">Style sheets</a>, Previous: <a rel="previous" accesskey="p" href="Large-projects.html#Large-projects">Large projects</a>, Up: <a rel="up" accesskey="u" href="Suggestions-for-writing-LilyPond-input-files.html#Suggestions-for-writing-LilyPond-input-files">Suggestions for writing LilyPond input files</a> <hr> </div> <h4 class="subsection">5.1.4 Saving typing with variables and functions</h4> <p><a name="index-variables-442"></a><a name="index-variables-443"></a> By this point, you've seen this kind of thing: <blockquote> <pre class="verbatim"> hornNotes = \relative c'' { c4 b dis c } \score { { \hornNotes } } </pre> <p> <a href="../8a/lily-718d27d0.ly"> <img align="middle" border="0" src="../8a/lily-718d27d0.png" alt="[image of music]"> </a> </p> </blockquote> <p>You may even realize that this could be useful in minimalist music: <blockquote> <pre class="verbatim"> fragmentA = \relative c'' { a4 a8. b16 } fragmentB = \relative c'' { a8. gis16 ees4 } violin = \new Staff { \fragmentA \fragmentA \fragmentB \fragmentA } \score { { \violin } } </pre> <p> <a href="../3b/lily-928ad868.ly"> <img align="middle" border="0" src="../3b/lily-928ad868.png" alt="[image of music]"> </a> </p> </blockquote> <p>However, you can also use these variables (also known as variables, macros, or (user-defined) command) for tweaks: <blockquote> <pre class="verbatim"> dolce = \markup{ \italic \bold dolce } padText = { \once \override TextScript #'padding = #5.0 } fthenp=_\markup{ \dynamic f \italic \small { 2nd } \hspace #0.1 \dynamic p } violin = \relative c'' { \repeat volta 2 { c4._\dolce b8 a8 g a b | \padText c4.^"hi there!" d8 e' f g d | c,4.\fthenp b8 c4 c-. | } } \score { { \violin } \layout{ragged-right=##t} } </pre> <p> <a href="../fb/lily-ace13d5f.ly"> <img align="middle" border="0" src="../fb/lily-ace13d5f.png" alt="[image of music]"> </a> </p> </blockquote> <p>These variables are obviously useful for saving typing. But they're worth considering even if you only use them once – they reduce complexity. Let's look at the previous example without any variables. It's a lot harder to read, especially the last line. <pre class="example">violin = \relative c'' { \repeat volta 2 { c4._\markup{ \italic \bold dolce } b8 a8 g a b | \once \override TextScript #'padding = #5.0 c4.^"hi there!" d8 e' f g d | c,4.\markup{ \dynamic f \italic \small { 2nd } \hspace #0.1 \dynamic p } b8 c4 c-. | } } </pre> <!-- TODO Replace the following with a better example -td --> <!-- Skylining handles this correctly without padText --> <p>So far we've seen static substitution – when LilyPond sees <code>\padText</code>, it replaces it with the stuff that we've defined it to be (ie the stuff to the right of <code>padtext=</code>). <p>LilyPond can handle non-static substitution, too (you can think of these as functions). <blockquote> <pre class="verbatim"> padText = #(define-music-function (parser location padding) (number?) #{ \once \override TextScript #'padding = #$padding #}) \relative c''' { c4^"piu mosso" b a b \padText #1.8 c4^"piu mosso" d e f \padText #2.6 c4^"piu mosso" fis a g } </pre> <p> <a href="../fd/lily-5fb6bbf8.ly"> <img align="middle" border="0" src="../fd/lily-5fb6bbf8.png" alt="[image of music]"> </a> </p> </blockquote> <p>Using variables is also a good way to reduce work if the LilyPond input syntax changes (see <a href="Updating-old-input-files.html#Updating-old-input-files">Updating old input files</a>). If you have a single definition (such as <code>\dolce</code>) for all your input files (see <a href="Style-sheets.html#Style-sheets">Style sheets</a>), then if the syntax changes, you only need to update your single <code>\dolce</code> definition, instead of making changes throughout every <code>.ly</code> file. <!-- footer_tag --><br><hr> <div class="node"> <p> Next: <a rel="next" accesskey="n" href="Style-sheets.html#Style-sheets">Style sheets</a>, Previous: <a rel="previous" accesskey="p" href="Large-projects.html#Large-projects">Large projects</a>, Up: <a rel="up" accesskey="u" href="Suggestions-for-writing-LilyPond-input-files.html#Suggestions-for-writing-LilyPond-input-files">Suggestions for writing LilyPond input files</a> </div> <div style="background-color: #e8ffe8; padding: 2; border: #c0ffc0 1px solid;"> <p> <font size="-1"> This page is for LilyPond-2.11.57 (development-branch). <br> <address> Report errors to <a href="http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs">http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs</a>. </address> <br> Your <a href="http://lilypond.org/web/devel/participating/documentation-adding">suggestions for the documentation</a> are welcome. </font> </p> </div> <P> Other languages: <a href="Saving-typing-with-variables-and-functions.fr.html">français</a>, <a href="Saving-typing-with-variables-and-functions.es.html">español</a>, <a href="Saving-typing-with-variables-and-functions.de.html">deutsch</a>. <BR> </P> </BODY></html>