I often want to write filenames for code.
Normally I write like this:
Filename
string = "This is example code"
Is this okay? Is there a standard Stack Overflow way of Markdown formatting for writing a filename?
This is ok if you write it the way you shown (the filename on one line, then on other lines code following). But if you have something like this:
In my viewcontroller.h I have:
some code there
I personally would like to see it as
In my
viewcontroller.h
I have:some code there
Since July 2021 (apparently) the previous method no longer works. Here's a new method based on tag stripping:
<h5 a><strong><code>hello_world.js</code></strong></h5>
``` js
alert("Hello world!");
```
Here the <h5 a>
tag gets stripped leaving its content "naked". Note that an attribute in <h5 a>
tag and a blank line after it are required!
You can make a very nice-looking named code block using small headers and inline code, like so:
#### **`hello_world.js`**
``` js
alert("Hello world!");
```
hello_world.js
alert("Hello world!");
I've accidentally found it in this post on Formatting Sandbox and just don't want this great trick to get lost there as I was unsuccessfully looking for this some time before.
Though not as good as above, you could also specify a file name via language-specific comment before your code:
# hello_world.sh
echo "Hello world!"
<blockquote> <code>code.js</code> <pre>...</pre> </blockquote>
####
+ ```
)
#
symbols in front of a line should correspond with the overall structure of the answer (although there is an argument for never using a single #
since that is for H1 formatting, and some believe this should only apply to the question title - personally, I think it equally makes sense for the N answers
label that's part of the page template, but....), and not simply be chosen for aesthetic purposes.
Commented
Mar 14 at 22:15
#
at least). This is needed to prevent the <code>
tag from being wrapped with a <p>
tag which has bottom padding. I would happily use the official method if there was one... [1/2]
Commented
Mar 15 at 6:57
I'd never thought about it until seeing nicael's answer, but I kind of like "quoting the file".
test.bat
@echo off setlocal enableDelayedExpansion set argument1=!%1! echo %argument1%>test.txt
file2.bat
@echo off echo %~1
After pounding my own head against my desk (and many others' desks) from wondering about this four years ago, I'm now writing all my Markdown with files as code
because...
code
; files are part of code
.code
, then it is already a file.cd
and a few others).code
, then all other non-executable files should also._
), which many files have in their names._
as an italics-string (literally _italics-string_
in Markdown).inline code
in general.
Commented
Mar 17 at 7:12
I tend to do something like:
requirements.txt
:
elasticsearch==7.0.0
elasticsearch-dsl==7.0.0
Bolding is fine, though I think that the header markup you've used may be a little too forceful. Large text could help, though, if you had a lot of (hopefully small!) snippets in your post. I'm not sure we need a standard for this: use what makes your post most easily read and understood.
I will say, however, that code markup is definitely not the proper choice. The name of a file is not an element of a program; marking it as such is semantically incorrect.
ArrayUtils.java
isn't the name for a class of utility methods for arrays, the asker is doing it wrong. main.js
, static.js
, etc.
Commented
Jan 10, 2015 at 20:41
`inline code`
, so includes standard input, test cases, relevant directories and file names and executables and constants and related configurations. I do it, and I see everyone else do it, and I teach new users to use the site by leaving a comment about using inline styles instead of italics, and they agree. Not sure what being parsed takes significance here, @Josh. Perhaps python2
myprogram.py foo bar
...
Commented
Jan 10, 2015 at 21:07
id_dsa.pub
, id_ecdsa.pub
or id_ed25519.pub
" inline with code formatting, that's what I meant because it's related to a keyfile. As for using italics for filenames, I guess they used a "traditional" BBCode forum with no code formatting ever, but I can't be sure.
Commented
Jan 10, 2015 at 21:16
oh this name has spaces.and.dots
which may be hard to read without the file in inline code style. Italics or bold don't solve the problem well since they don't "bind" the parts of the file name as closely as the code style box.