{"id":1176,"date":"2015-05-22T11:40:22","date_gmt":"2015-05-22T16:40:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=1176"},"modified":"2016-08-04T09:17:58","modified_gmt":"2016-08-04T14:17:58","slug":"including-external-code-in-published-document","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2015\/05\/22\/including-external-code-in-published-document\/","title":{"rendered":"Including External Code in Published Document"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>When I wanted to show you a code snippet in the past, I displayed the code in the text of my blog post.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#be3c4a37-2d77-4e18-a07a-28238fbb6446\">How I Did It<\/a><\/li><li><a href=\"#acedd5a0-fe58-4b75-b791-27db984e6a5b\">How I Do It Now<\/a><\/li><li><a href=\"#7a5b2940-977c-4359-a84f-f2dbf21689de\">Let's Try It Out!<\/a><\/li><li><a href=\"#620336fe-3280-4428-8136-a252d05eb2bd\">Will This Inclusive Feature Help You?<\/a><\/li><\/ul><\/div><h4>How I Did It<a name=\"be3c4a37-2d77-4e18-a07a-28238fbb6446\"><\/a><\/h4><p>To do so, I often used the function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/type.html\"><tt>type<\/tt><\/a>, or sometimes <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/dbtype.html\"><tt>dbtype<\/tt><\/a> (if I wanted to refer to line numbers).  In this way, I did not require you to download code from elsewhere to see what I was discussing.<\/p><h4>How I Do It Now<a name=\"acedd5a0-fe58-4b75-b791-27db984e6a5b\"><\/a><\/h4><p>Now using R2015a I can take advantage of a new feature of the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/marking-up-matlab-comments-for-publishing.html\">markup language<\/a> for publish.  Using the <tt>include<\/tt> markup allows me to include external content. How do I do this?  With the delimiters<\/p><pre>             &lt;include&gt;fileOfInterest.m&lt;\/include&gt;<\/pre><p>Not only that, but the content is included with the proper syntax highlighting for MATLAB code!<\/p><h4>Let's Try It Out!<a name=\"7a5b2940-977c-4359-a84f-f2dbf21689de\"><\/a><\/h4><p>Here's some code from an older post of mine of methods for computing Fibonacci numbers.<\/p><pre class=\"language-matlab\">\r\n<span class=\"keyword\">function<\/span> f = fibrec(n)\r\n<span class=\"comment\">%FIBREC Recursive function for n Fibonacci numbers.<\/span>\r\n\r\n<span class=\"comment\">% Minimize the error checking so we don't bog down in it. I have included it<\/span>\r\n<span class=\"comment\">%in comments instead.<\/span>\r\n<span class=\"comment\">% if ~isscalar(n) | ~isreal(n) | n&lt;0 | fix(n)~=n<\/span>\r\n<span class=\"comment\">%    error('ArtBlog:fibrec:MBRealPosInt','N must be a real positive integer')<\/span>\r\n<span class=\"comment\">% end<\/span>\r\n<span class=\"keyword\">if<\/span> n == 1,\r\n    f = 1;   <span class=\"comment\">% First element is 1.<\/span>\r\n    <span class=\"keyword\">return<\/span>;\r\n<span class=\"keyword\">elseif<\/span> n == 2\r\n    f = [1 1];  <span class=\"comment\">% First two elements are 1.<\/span>\r\n<span class=\"keyword\">else<\/span>\r\n    <span class=\"comment\">% Call fibrec with previous result and compute next one from it.<\/span>\r\n    fm1 = fibrec(n-1); \r\n    f = [fm1 fm1(end)+fm1(end-1)];\r\n<span class=\"keyword\">end<\/span>\r\n\r\n<\/pre><h4>Will This Inclusive Feature Help You?<a name=\"620336fe-3280-4428-8136-a252d05eb2bd\"><\/a><\/h4><p>Let us know how <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=1176#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_e8e435f197f24c2086192130db49f10c() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='e8e435f197f24c2086192130db49f10c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e8e435f197f24c2086192130db49f10c';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2015 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_e8e435f197f24c2086192130db49f10c()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015a<br><\/p><\/div><!--\r\ne8e435f197f24c2086192130db49f10c ##### SOURCE BEGIN #####\r\n%% Including External Code in Published Document\r\n% When I wanted to show you a code snippet in the past, I displayed\r\n% the code in the text of my blog post.\r\n%% How I Did It\r\n% To do so, I often used the function\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/type.html |type|>, or sometimes\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/dbtype.html |dbtype|> (if I\r\n% wanted to refer to line numbers).  In this way, I did not require you to\r\n% download code from elsewhere to see what I was discussing.\r\n%% How I Do It Now\r\n% Now using R2015a I can take advantage of a new feature of the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/marking-up-matlab-comments-for-publishing.html\r\n% markup language> for publish.  Using the |<include>| markup allows me to\r\n% include external content. How do I do this?  With the delimiters\r\n%\r\n%               <include>fileOfInterest.m<\/include>\r\n%\r\n% Not only that, but the content is included with the proper syntax\r\n% highlighting for MATLAB code!\r\n%% Let's Try It Out!\r\n% Here's some code from an older post of mine of methods for computing\r\n% Fibonacci numbers.\r\n%\r\n% <include>fibrec.m<\/include>\r\n%\r\n%% Will This Inclusive Feature Help You?\r\n% Let us know how <https:\/\/blogs.mathworks.com\/loren\/?p=1176#respond here>.\r\n\r\n##### SOURCE END ##### e8e435f197f24c2086192130db49f10c\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>When I wanted to show you a code snippet in the past, I displayed the code in the text of my blog post.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2015\/05\/22\/including-external-code-in-published-document\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,13],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1176"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=1176"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1176\/revisions"}],"predecessor-version":[{"id":1970,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/1176\/revisions\/1970"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=1176"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=1176"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=1176"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}